Python equivalent of lua bit32.bxor

Question:

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…

Asked By: River

||

Answers:

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

>>>8 ^ 16
24
Answered By: zaki98

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

result = 10 ^ 20 ^ 30 ^ 40
print(result)  # Output: 40
Answered By: tomerar

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.

Answered By: kkmonlee
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.