Run any bitwise operation on two integers and see the result in decimal, binary and hex, with the operands lined up bit by bit.
| A | ||
| B | ||
| Result |
| Operation | Rule, bit by bit |
|---|---|
| XOR ^ | 1 when the two bits differ, 0 when they match |
| AND & | 1 only when both bits are 1 |
| OR | | 1 when at least one bit is 1 |
| NOT ~ | Flips every bit; on signed integers ~x equals −x−1 |
| Left shift << | Moves bits left, filling with zeros; each place doubles the value |
| Right shift >> | Moves bits right, keeping the sign bit; each place halves, rounding down |
XOR is its own inverse: (a ^ b) ^ b = a. Applying the same key twice returns the original, which is the basis of the simplest stream ciphers, of swapping two variables without a temporary, and of parity and checksum schemes. It is also the only one of the three binary operations that loses no information — given the result and either operand you can always recover the other, which is impossible for AND and OR.
Check the reversibility: 49 XOR 13 = 60, back where we started.
Exclusive OR. Comparing two numbers bit by bit, each output bit is 1 when the two input bits differ and 0 when they are the same.
49. In binary, 60 is 00111100 and 13 is 00001101; the bits differ in four columns, giving 00110001, which is 49 in decimal and 0x31 in hex.
Because it is its own inverse. Applying the same key twice with XOR returns the original data, so encryption and decryption are the same operation, and no information is lost in either direction.
OR gives 1 when at least one bit is 1, including when both are. XOR gives 1 only when exactly one bit is 1, so it returns 0 when both bits are 1.
On signed integers, NOT x equals minus x minus one. So NOT 5 is minus 6, because flipping every bit in two's complement representation produces that result.