0Pricing
C Academy · Lesson

Bitwise Operators

AND, OR, XOR, NOT.

Working at the Bit Level

Integers are stored as sequences of bits. C lets you operate on those bits directly with the bitwise operators.

The four core operators are AND &, OR |, XOR ^, and NOT ~.

#include <stdio.h>

int main(void) {
    unsigned a = 12;
    unsigned b = 10;
    printf("a & b = %u\n", a & b);
    return 0;
}

Bitwise AND

The & operator sets a result bit to 1 only when both input bits are 1.

For 12 (1100) and 10 (1010), the result is 8 (1000).

#include <stdio.h>

int main(void) {
    unsigned a = 12, b = 10;
    printf("%u & %u = %u\n", a, b, a & b);
    return 0;
}

All lessons in this course

  1. Bitwise Operators
  2. Shifts
  3. Bit Masks and Flags
  4. Practical Bit Tricks
← Back to C Academy