Created
April 20, 2013 00:33
-
-
Save startling/5424169 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def bit_iter(bits, n): | |
| "Iterate over the bits of an integer, padding it to `n` digits." | |
| return ((bits >> i) & 1 for i in xrange(n - 1, -1, -1)) | |
| def unsigned_bin(x, n=32): | |
| "Print the unsigned binary representation of an integer." | |
| return "".join(repr(y) for y in bit_iter(x, n)) | |
| print unsigned_bin(0b101, 8) # '00000101' | |
| print usngined_bin(~0b101, 8) # '11111010' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment