Timeline for Converting integer to binary in Python
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 15, 2023 at 10:44 | comment | added | Mew | This is the only answer that supports negative numbers (which, since Python stores them in 2's complement, are output in 2's complement format with this method). | |
| Sep 22, 2021 at 21:38 | comment | added | s3dev |
Very nice! As an observation, reversed could be removed by using range(7,-1,-1); albeit more ‘pure’, but perhaps less readable/intuitive.
|
|
| Feb 18, 2018 at 12:18 | comment | added | Jongware |
@Gregory: it shifts the bits in x to the right and ANDs it with 1, effectively extracting one bit (0 or 1) at a time.
|
|
| S Feb 18, 2018 at 11:41 | history | suggested | marbel82 | CC BY-SA 3.0 |
Important information that works also with negative numbers
|
| Feb 18, 2018 at 10:24 | review | Suggested edits | |||
| S Feb 18, 2018 at 11:41 | |||||
| Sep 24, 2016 at 10:17 | comment | added | Gregory | Nice method. But I couldn't understand what this part of your code is doing: str((x >> i) & 1) | |
| May 2, 2012 at 10:07 | history | answered | sobel | CC BY-SA 3.0 |