Created
December 16, 2015 00:02
-
-
Save pgreze/a7ab313f4916fff1337e to your computer and use it in GitHub Desktop.
Binary operations with Python
This file contains 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 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