Return a list of all power-of-two integers contained in a flag.
+ :func:`enum.bin`
+
+ Like built-in :func:`bin`, except negative values are represented in
+ two's complement, and the leading bit always indicates sign
+ (``0`` implies positive, ``1`` implies negative).
+
.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
.. versionadded:: 3.11 ``StrEnum``, ``EnumCheck``, ``ReprEnum``, ``FlagBoundary``, ``property``, ``member``, ``nonmember``, ``global_enum``, ``show_flag_values``
.. versionadded:: 3.11
+.. function:: bin(num, max_bits=None)
+
+ Like built-in :func:`bin`, except negative values are represented in
+ two's complement, and the leading bit always indicates sign
+ (``0`` implies positive, ``1`` implies negative).
+
+ >>> import enum
+ >>> enum.bin(10)
+ '0b0 1010'
+ >>> enum.bin(~10) # ~10 is -11
+ '0b1 0101'
+
+ .. versionadded:: 3.10
+
---------------
Notes
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
+ See also :func:`enum.bin` to represent negative values as twos-complement.
+
See also :func:`format` for more information.
def bin(num, max_bits=None):
"""
Like built-in bin(), except negative values are represented in
- twos-compliment, and the leading bit always indicates sign
+ twos-complement, and the leading bit always indicates sign
(0=positive, 1=negative).
>>> bin(10)
'0b1 0101'
"""
+ num = num.__index__()
ceiling = 2 ** (num).bit_length()
if num >= 0:
s = bltns.bin(num + ceiling).replace('1', '0', 1)