Skip to content

Commit b4a84bc

Browse files
guociStanFromIrelandblurb-it[bot]
authored
[3.13] gh-140806: add docs for enum.bin function (#140807) (#143740)
(cherry picked from commit 7f50a5f) 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>
1 parent b74e3a4 commit b4a84bc

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

Doc/library/enum.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ Module Contents
153153

154154
Return a list of all power-of-two integers contained in a flag.
155155

156+
:func:`enum.bin`
157+
158+
Like built-in :func:`bin`, except negative values are represented in
159+
two's complement, and the leading bit always indicates sign
160+
(``0`` implies positive, ``1`` implies negative).
161+
156162

157163
.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
158164
.. versionadded:: 3.11 ``StrEnum``, ``EnumCheck``, ``ReprEnum``, ``FlagBoundary``, ``property``, ``member``, ``nonmember``, ``global_enum``, ``show_flag_values``
@@ -1034,6 +1040,19 @@ Utilities and Decorators
10341040

10351041
.. versionadded:: 3.11
10361042

1043+
.. function:: bin(num, max_bits=None)
1044+
1045+
Like built-in :func:`bin`, except negative values are represented in
1046+
two's complement, and the leading bit always indicates sign
1047+
(``0`` implies positive, ``1`` implies negative).
1048+
1049+
>>> import enum
1050+
>>> enum.bin(10)
1051+
'0b0 1010'
1052+
>>> enum.bin(~10) # ~10 is -11
1053+
'0b1 0101'
1054+
1055+
.. versionadded:: 3.10
10371056

10381057
---------------
10391058

Doc/library/functions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ are always available. They are listed here in alphabetical order.
138138
>>> f'{14:#b}', f'{14:b}'
139139
('0b1110', '1110')
140140

141+
See also :func:`enum.bin` to represent negative values as twos-complement.
142+
141143
See also :func:`format` for more information.
142144

143145

Lib/enum.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def show_flag_values(value):
130130
def bin(num, max_bits=None):
131131
"""
132132
Like built-in bin(), except negative values are represented in
133-
twos-compliment, and the leading bit always indicates sign
133+
twos-complement, and the leading bit always indicates sign
134134
(0=positive, 1=negative).
135135
136136
>>> bin(10)
@@ -139,6 +139,7 @@ def bin(num, max_bits=None):
139139
'0b1 0101'
140140
"""
141141

142+
num = num.__index__()
142143
ceiling = 2 ** (num).bit_length()
143144
if num >= 0:
144145
s = bltns.bin(num + ceiling).replace('1', '0', 1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add documentation for :func:`enum.bin`.

0 commit comments

Comments
 (0)