Last active
August 29, 2015 14:07
-
-
Save akulakov/b2d6c1f7a923dfdaa6b7 to your computer and use it in GitHub Desktop.
bitwise and
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 binp(x): | |
print("{:3} {:08b}".format(x, x)) | |
def bitwise_and(a, b): | |
result = 0 | |
mask = 1 | |
while a and b: | |
if (a % 2) == (b % 2) == 1: | |
result += mask | |
mask <<= 1 | |
a >>= 1 | |
b >>= 1 | |
return result | |
a, b = 101, 115 | |
binp(a); binp(b) | |
print("out:") | |
binp(bitwise_and(a, b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment