This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow bin() to have an optional "Total Bits" argument.
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.1
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: MechPaul, r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2009-04-14 01:01 by MechPaul, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg85956 - (view) Author: (MechPaul) Date: 2009-04-14 01:01
As it stands right now, bin() only shows the minimum number of bits to
display the number, however I think having a "Total Bits" argument would
be very, very useful for bin().

bin(value, [Total bits to display])

"Total bits to display" is an integer.

Here's how it should work:

>>> bin(0x7F)
'0b1111111'
>>> #Note, there are seven 1's there to represent the value 127
>>> bin(0x7F, 8)
'0b01111111'
>>> #Now there are eight bits displayed.
>>> bin(0xFF, 16)
'0b0000000011111111'
>>> #This makes it a lot easier to read and compare binary numbers!
msg85957 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-04-14 01:28
A better (more flexible) way already exists (as of 2.6) to accomplish
your goal:

>>> "0b{0:08b}".format(127)
'0b01111111'
msg85959 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-04-14 03:26
I concur with closing this one.
History
Date User Action Args
2022-04-11 14:56:47adminsetgithub: 49999
2009-04-14 03:26:28rhettingersetnosy: + rhettinger
messages: + msg85959
2009-04-14 01:28:20r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg85957

resolution: rejected
stage: resolved
2009-04-14 01:01:18MechPaulcreate