Skip to content

Commit 135199e

Browse files
committed
add docs for enum.bin function
1 parent abd19ed commit 135199e

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-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=positive, 1=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,20 @@ 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=positive, 1=negative).
1048+
1049+
>>> from enum import enum
1050+
>>> enum.bin(10)
1051+
'0b0 1010'
1052+
>>> enum.bin(~10) # ~10 is -11
1053+
'0b1 0101'
1054+
1055+
.. versionadded:: 3.11
1056+
10371057
---------------
10381058

10391059
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
@@ -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)

0 commit comments

Comments
 (0)