Labs ICT
Pro Login

Bitwise Operators

Bitwise operators work on the individual bits of a number. These are not for everyday math — they are for when you need to manipulate data at the lowest level. Embedded programmers, graphics programmers, and cryptography developers use these constantly.

The Bitwise Operators

  • & — AND. A bit is 1 only if both bits are 1.
  • | — OR. A bit is 1 if at least one bit is 1.
  • ^ — XOR. A bit is 1 if the bits are different.
  • ~ — NOT. Flips every bit.
  • << — Left shift. Shifts bits left, fills with zeros.
  • >> — Right shift. Shifts bits right.

What Does This Look Like?

Take the number 5 (binary 0101) and 3 (binary 0011):

  • 5 & 3 = 1 (0001)
  • 5 | 3 = 7 (0111)
  • 5 ^ 3 = 6 (0110)
  • ~5 = -6 (bits flipped on a 32-bit system)
  • 5 << 1 = 10 (1010) — shift left multiplies by 2
  • 5 >> 1 = 2 (0010) — shift right divides by 2

#include 

int main() {
  int a = 5;
  int b = 3;

  printf("a & b = %d\n", a & b);
  printf("a | b = %d\n", a | b);
  printf("a ^ b = %d\n", a ^ b);
  printf("~a = %d\n", ~a);
  printf("a << 1 = %d\n", a << 1);
  printf("a >> 1 = %d\n", a >> 1);
  return 0;
}
    
Try it Yourself →