Skip to content

Instantly share code, notes, and snippets.

@startling
Created April 20, 2013 00:33
Show Gist options
  • Save startling/5424169 to your computer and use it in GitHub Desktop.
Save startling/5424169 to your computer and use it in GitHub Desktop.
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