Implement int.bit_length() and int.bit_count()
Merged
GA
21d ago+191+191+012345678901234567890123456789-5-5-0123456789
Summary
Implements int.bit_length() and int.bit_count() methods across machine integers (i64), interned large integers, and arbitrary-precision heap integers (LongInt), as well as booleans:
int.bit_length(): Returns the number of bits necessary to represent an integer in binary, excluding sign and leading zeros ((0).bit_length() == 0).int.bit_count(): Returns the number of ones in the binary representation of the absolute value of the integer ((0).bit_count() == 0).- Supports
i64(small ints),LongInt(BigIntmagnitude bit operations), andbool. - Dispatches zero-argument checks matching CPython error handling (
TypeError: int.bit_length() takes no arguments (1 given)).
Addresses part of #1060.
Summary by cubic
Implements int.bit_length() and int.bit_count() for machine integers (i64), interned big integers, heap LongInt values, and booleans, addressing part of #1060.
Behavior
bit_length()returns the number of bits needed to represent the number in binary, excluding sign and leading zeros so(0).bit_length() == 0.bit_count()returns the number of ones in the binary representation of the absolute value, so(-37).bit_count() == (37).bit_count() == 3.- Both methods raise
TypeErrorwhen given arguments andAttributeErrorfor unknown attributes, matching CPython error handling.