Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd behavior of Python operator.xor

I am working on an encryption puzzle and am needing to take the exclusive or of two binary numbers (I'm using the operator package in Python). If I run operator.xor(1001111, 1100001) for instance I get the very weird output 2068086. Why doesn't it return 0101110 or at least 101110?

like image 262
dsaxton Avatar asked Dec 02 '22 15:12

dsaxton


2 Answers

Because Python doesn't see that as binary numbers. Instead use:

operator.xor(0b1001111, 0b1100001)
like image 139
Ewoud Avatar answered Dec 16 '22 16:12

Ewoud


The calculated answer is using the decimal values you provided, not their binary appearance. What you are really asking is...

1001111 ^ 1100001

When you mean is 79 ^ 97. Instead try using the binary literals as so...

0b1001111 ^ 0b1100001

See How do you express binary literals in Python? for more information.

like image 42
Hunter Larco Avatar answered Dec 16 '22 18:12

Hunter Larco