C Academy · Lesson

Bitwise Operators

AND, OR, XOR, NOT.

Lesson 1 of 413 steps

Bitwise Operators is a free C Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.

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;
}

Bitwise OR

The | operator sets a result bit to 1 when at least one input bit is 1.

12 (1100) OR 10 (1010) is 14 (1110).

#include <stdio.h>

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

Bitwise XOR

The ^ operator (exclusive OR) sets a result bit to 1 when the input bits differ.

12 (1100) XOR 10 (1010) is 6 (0110).

#include <stdio.h>

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

Bitwise NOT

The ~ operator flips every bit. On unsigned values, ~0 is all ones, the maximum value.

#include <stdio.h>

int main(void) {
    unsigned char x = 0;
    printf("~0 as 8-bit = %u\n", (unsigned char)~x);
    return 0;
}

Printing Bits

A helper that prints the binary form of a byte makes bitwise results easier to understand. We test each bit from high to low.

#include <stdio.h>

void print_bits(unsigned char v) {
    for (int i = 7; i >= 0; i--) {
        putchar((v >> i) & 1 ? '1' : '0');
    }
    putchar('\n');
}

int main(void) {
    print_bits(12);
    print_bits(10);
    return 0;
}

XOR Swap

XOR has a neat property: you can swap two integers without a temporary variable.

Apply XOR three times to exchange the values.

#include <stdio.h>

int main(void) {
    int a = 5, b = 9;
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    printf("a=%d b=%d\n", a, b);
    return 0;
}

XOR with Itself

Any value XORed with itself is 0, and any value XORed with 0 is unchanged.

These identities power the XOR swap and many tricks.

#include <stdio.h>

int main(void) {
    int x = 42;
    printf("x ^ x = %d\n", x ^ x);
    printf("x ^ 0 = %d\n", x ^ 0);
    return 0;
}

Bitwise vs Logical

Do not confuse & with && or | with ||.

  • Bitwise operators work on every bit of the operands.
  • Logical operators evaluate truthiness and return 0 or 1.
#include <stdio.h>

int main(void) {
    printf("2 & 1 = %d\n", 2 & 1);
    printf("2 && 1 = %d\n", 2 && 1);
    return 0;
}

Combining Operators

Bitwise operators combine to express complex conditions. Here we keep bits that are in a but not in b.

#include <stdio.h>

int main(void) {
    unsigned a = 0b1111;
    unsigned b = 0b0101;
    unsigned only_a = a & ~b;
    printf("only_a = %u\n", only_a);
    return 0;
}

Checking Parity with XOR

XORing all bits together tells you the parity (whether the number of set bits is odd). Fold the value down repeatedly.

#include <stdio.h>

int main(void) {
    unsigned v = 0b1011;
    int parity = 0;
    while (v) {
        parity ^= (v & 1);
        v >>= 1;
    }
    printf("parity = %d\n", parity);
    return 0;
}

Quick Check

Test your knowledge of bitwise operators.

Recap

You learned the bitwise operators:

  • & AND keeps bits set in both operands.
  • | OR keeps bits set in either operand.
  • ^ XOR keeps bits that differ.
  • ~ NOT flips all bits.
  • Bitwise differs from logical && and ||.
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Bitwise Operators” lesson free?

Yes — the full text of “Bitwise Operators” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.

What will I learn in “Bitwise Operators”?

AND, OR, XOR, NOT. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Bitwise Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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