MCPcopy Index your code
hub / github.com/RustPython/RustPython / bin

Function bin

Lib/enum.py:129–152  ·  view source on GitHub ↗

Like built-in bin(), except negative values are represented in twos-complement, and the leading bit always indicates sign (0=positive, 1=negative). >>> bin(10) '0b0 1010' >>> bin(~10) # ~10 is -11 '0b1 0101'

(num, max_bits=None)

Source from the content-addressed store, hash-verified

127 return list(_iter_bits_lsb(value))
128
129def bin(num, max_bits=None):
130 """
131 Like built-in bin(), except negative values are represented in
132 twos-complement, and the leading bit always indicates sign
133 (0=positive, 1=negative).
134
135 >>> bin(10)
136 '0b0 1010'
137 >>> bin(~10) # ~10 is -11
138 '0b1 0101'
139 """
140
141 num = num.__index__()
142 ceiling = 2 ** (num).bit_length()
143 if num >= 0:
144 s = bltns.bin(num + ceiling).replace('1', '0', 1)
145 else:
146 s = bltns.bin(~num ^ (ceiling - 1) + ceiling)
147 sign = s[:3]
148 digits = s[3:]
149 if max_bits is not None:
150 if len(digits) < max_bits:
151 digits = (sign[-1] * max_bits + digits)[-max_bits:]
152 return "%s %s" % (sign, digits)
153
154class _not_given:
155 def __repr__(self):

Callers 2

_missing_Method · 0.70
builtin_bin.pyFile · 0.50

Calls 4

lenFunction · 0.85
__index__Method · 0.45
bit_lengthMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected