MCPcopy Index your code
hub / github.com/python/cpython / _int2digits

Function _int2digits

Lib/_pylong.py:465–493  ·  view source on GitHub ↗

Decompose non-negative int a into base 2**n Input: a is a non-negative integer Output: List of the digits of a in base 2**n in little-endian order, meaning the most significant digit is last. The most significant digit is guaranteed to be non-zero. If a is 0 t

(a, n)

Source from the content-addressed store, hash-verified

463
464
465def _int2digits(a, n):
466 """Decompose non-negative int a into base 2**n
467
468 Input:
469 a is a non-negative integer
470
471 Output:
472 List of the digits of a in base 2**n in little-endian order,
473 meaning the most significant digit is last. The most
474 significant digit is guaranteed to be non-zero.
475 If a is 0 then the output is an empty list.
476
477 """
478 a_digits = [0] * ((a.bit_length() + n - 1) // n)
479
480 def inner(x, L, R):
481 if L + 1 == R:
482 a_digits[L] = x
483 return
484 mid = (L + R) >> 1
485 shift = (mid - L) * n
486 upper = x >> shift
487 lower = x ^ (upper << shift)
488 inner(lower, L, mid)
489 inner(upper, mid, R)
490
491 if a:
492 inner(a, 0, len(a_digits))
493 return a_digits
494
495
496def _digits2int(digits, n):

Callers 1

_divmod_posFunction · 0.85

Calls 2

bit_lengthMethod · 0.80
innerFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…