0
Can someone help me understand Binary XOR
Why is the output 0b10 a = 0b1011 b = 0b1001 print(bin(a ^ b)) The third element in both a and b are 1. So shouldn’t the output be 0b00 since it’s XOR which is 1 ^ 1 = 0
1 ответ
+ 8
XOR returns true if and only if either of bits are 1 but not both.
So
1^1 = 0
0^0=0
But
1^0=1
So in your example
Leftmost bits are both 1 so 1^1=0
Then second leftmost bits are both 0, so 0^0=0
Third leftmost bits, only one is set, so 1^0=1
And finally last bits are both 1, so 1^1=0
So the result is 0b0010
But then we remove first two zeros as they are meaningless, resulting in 0b10
(it's like when you write 8 instead of 08 or 8.00)