Created
May 5, 2023 09:59
-
-
Save igorvanloo/44068c3d84975c40c0c6e8ea5138edce to your computer and use it in GitHub Desktop.
xorproduct
This file contains hidden or 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 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