Skip to content

Instantly share code, notes, and snippets.

@akulakov
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akulakov/b2d6c1f7a923dfdaa6b7 to your computer and use it in GitHub Desktop.
Save akulakov/b2d6c1f7a923dfdaa6b7 to your computer and use it in GitHub Desktop.
bitwise and
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