Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Created May 5, 2023 09:59
Show Gist options
  • Save igorvanloo/44068c3d84975c40c0c6e8ea5138edce to your computer and use it in GitHub Desktop.
Save igorvanloo/44068c3d84975c40c0c6e8ea5138edce to your computer and use it in GitHub Desktop.
xorproduct
def xorProduct(x, y):
prod = 0
#Note that 0 ^ x = x, ^ = XOR
while y != 0:
if y % 2 == 1:
#If the first bit of y is a 1, prod = prod ^ x
prod ^= x
#Then we shift x one bit to the left, essentially adds a 0 behind x
x <<= 1
#And we push y one bit to the right, essentially removes the first bit
y >>= 1
return prod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment