Skip to content

Instantly share code, notes, and snippets.

@pgreze
Created December 16, 2015 00:02
Show Gist options
  • Save pgreze/a7ab313f4916fff1337e to your computer and use it in GitHub Desktop.
Save pgreze/a7ab313f4916fff1337e to your computer and use it in GitHub Desktop.
Binary operations with Python
>>> def show(i): print(''.join(("1" if i & mask else "0") for mask in (0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01)))
...
>>> show(0x40)
01000000
>>> show(1)
00000001
>>> show(24)
00011000
>>> show(ord('a'))
01100001
>>> show(1 | 0x80)
10000001
>>> show(ord('a') & 0x40)
01000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment