Detailed explanation of XOR operation

Detailed explanation of XOR operation

definition

For the two data involved in the operation, the ^ operation is performed according to the binary digits. If the two corresponding values ​​are the same, the result is 0, otherwise it is 1

1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
1 ^ 1 = 0

characteristic

The XOR ^ operation can only be used for numeric values ​​(integer)

x ^ 0 = x
x ^ x = 0

use

  • Swap two values ​​without using a temporary variable
a = a ^ b;
b = b ^ a;
a = a ^ b;
  • Checks if two values ​​are equal
((a ^ b) == 0)

Summarize

insert image description here

Guess you like

Origin blog.csdn.net/weixin_42202992/article/details/132075289