Bitwise Pt. 3

Bitwise XOR (^) Operator

Inclusive OR

  • Either A is 1 or B is 1 or both are 1, then the output is 1.
  • Including Both
ABA OR B
000
011
101
111

Exclusive OR (XOR)

  • Either A is 1 or B is 1 then the output is 1 but when both A and B are 1 then output is 0.
  • Excluding Both
ABA XOR B
000
011
101
110
  • Bitwise XOR (^) is binary Operator. It takes two numbers and perform bitwise XOR.
  • Result of XOR is 1 when two bits are different, otherwise the result is 0.
0 1 1 1   <- 7
0 1 0 0   <- 4
0 0 1 1   <- 3

7 ^ 4 = 3

Swap using XOR


#include <stdio.h>

int main() {
  int a = 4, b = 3;
  a = a ^ b;
  b = a ^ b;
  a = a ^ b;

  printf("After XOR, a = %d and b = %d", a, b);
  return 0;
}