[3.14] gh-140806: add docs for enum.bin function (GH-140807) (#143726)

Co-authored-by: Guo Ci <zguoci@gmail.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2026-01-12 13:01:01 +01:00 committed by GitHub
parent dcf499f95f
commit 46594d4fbc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 1 deletions

View file

@ -153,6 +153,12 @@ Module Contents
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``
@ -1035,6 +1041,20 @@ Utilities and Decorators
.. 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

View file

@ -138,6 +138,8 @@ are always available. They are listed here in alphabetical order.
>>> 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.

View file

@ -129,7 +129,7 @@ def show_flag_values(value):
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)
@ -138,6 +138,7 @@ def bin(num, max_bits=None):
'0b1 0101'
"""
num = num.__index__()
ceiling = 2 ** (num).bit_length()
if num >= 0:
s = bltns.bin(num + ceiling).replace('1', '0', 1)

View file

@ -0,0 +1 @@
Add documentation for :func:`enum.bin`.