Skip to content

Commit 46594d4

Browse files
miss-islingtonguociStanFromIrelandblurb-it[bot]
authored
[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>
1 parent dcf499f commit 46594d4

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

Doc/library/enum.rst

Lines changed: 20 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``
@@ -1035,6 +1041,20 @@ Utilities and Decorators
10351041

10361042
.. versionadded:: 3.11
10371043

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

10401060
Notes

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
@@ -129,7 +129,7 @@ def show_flag_values(value):
129129
def bin(num, max_bits=None):
130130
"""
131131
Like built-in bin(), except negative values are represented in
132-
twos-compliment, and the leading bit always indicates sign
132+
twos-complement, and the leading bit always indicates sign
133133
(0=positive, 1=negative).
134134
135135
>>> bin(10)
@@ -138,6 +138,7 @@ def bin(num, max_bits=None):
138138
'0b1 0101'
139139
"""
140140

141+
num = num.__index__()
141142
ceiling = 2 ** (num).bit_length()
142143
if num >= 0:
143144
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)