Python equivalent of lua bit32.bxor

149 views Asked by At

I am trying to "convert" a lua script into a python script, but the lua script seems to have a library named bit32, and of which it uses the bxor function. Is there any python equivalent of the bit32.bxor function?

I have searched multiple times on stackoverflow and google for what the equivalent is, but i didn't find any. Maybe I'm just searching the wrong stuff...

3

There are 3 answers

0
zaki98 On BEST ANSWER

Is the script trying to calculate Exclusive OR of 2 integers ? if so you can use the bitwise operator ^

>>>8 ^ 16
24
1
tomerar On

You can use the ^ operator to perform bitwise exclusive XOR.

result = 10 ^ 20 ^ 30 ^ 40
print(result)  # Output: 40
0
kkmonlee On

Python provides bitwise XOR using the bitwise operator ^. However, you can use a function if you want, using the operator.xor(a, b) function.

You can also use NumPy's bitwise XOR function as an equivalent.