Skip to content

Instantly share code, notes, and snippets.

@Jim-Holmstroem
Created March 28, 2012 07:01
Show Gist options
  • Save Jim-Holmstroem/2224397 to your computer and use it in GitHub Desktop.
Save Jim-Holmstroem/2224397 to your computer and use it in GitHub Desktop.
int to list of bits in Python
def int2bits(i,n,zero_element=0,one_element=1):
return list((zero_element,one_element)[i>>j & 1] for j in xrange(n-1,-1,-1))
for i in range(16):
print int2bits(i,4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment