Part 5 — Chapter 21

Chapter 21: Bitwise Operators

Bitwise operators perform operations directly on individual bits of data. Unlike arithmetic or logical operators, bitwise operators work at the binary level, making them extremely important in low-level programming and embedded systems.

They are commonly used for:

  • hardware register control
  • masking specific bits
  • setting or clearing flags
  • communication protocols
  • memory-efficient operations

Binary Concept

Computers store all data internally in binary form using bits (0 and 1). For example:

Decimal 5 → 00000101
Decimal 3 → 00000011

Bitwise operators manipulate these bits directly, bit-by-bit.

Bitwise AND (&)

The AND operator sets a bit to 1 only if both corresponding bits are 1.

00000101(5)
00000011(3)
00000001Result: 1
Bitwise OR (|)

The OR operator sets a bit to 1 if either of the corresponding bits is 1.

00000101(5)
00000011(3)
00000111Result: 7
Bitwise XOR (^)

The XOR (exclusive OR) operator sets a bit to 1 only when the corresponding bits are different.

00000101(5)
00000011(3)
00000110Result: 6
Bitwise NOT (~)

The NOT operator is a unary operator that flips all bits. Inverting 1 to 0 and 0 to 1.

00000101(5)
11111010(~5)
Shift Operations (<< , >>)

Shift operators move all bits in a number to the left or right by a specified number of positions.

Left Shift (<<)

The left shift operator shifts bits toward the left. Each left shift generally multiplies the decimal value by 2.

00000101 << 1(5 shifted by 1)
00001010Result: 10

Right Shift (>>)

The right shift operator shifts bits toward the right. Each right shift generally divides the decimal value by 2.

00000101 >> 1(5 shifted by 1)
00000010Result: 2
Hands-on Demonstration
bitwise.c
C
#include <stdio.h>

int main()
{
    int a = 5; // 00000101
    int b = 3; // 00000011

    printf("AND: %d\n", a & b);
    printf("OR:  %d\n", a | b);
    printf("XOR: %d\n", a ^ b);

    return 0;
}

Console Output

AND: 1
OR:  7
XOR: 6

How It Works

Bitwise operators compare corresponding bits of two values and generate a new binary result based on the operation being performed. These operations happen directly at the hardware binary level inside the processor, which makes them highly computational efficient.

Important Points

  • Bitwise operators work directly on binary data
  • & performs bitwise AND
  • | performs bitwise OR
  • ^ performs bitwise XOR
  • ~ inverts all bits
  • << and >> shift bits left or right

Embedded Focus

Bitwise operators are fundamental in embedded programming because hardware registers are controlled at the bit level. They are used for peripheral control, status flags, communication protocols, interrupt handling, and memory-efficient embedded programming.

hardware.c
C
// Setting the 3rd bit of a GPIO register
PORTA |= (1 << 3); 

This operation sets bit 3 of a hardware register without affecting any other bits in the register, a practice that occurs thousands of times in real-world firmware.

← Chapter 20: Multi-file Projects Chapter 22: Registers & Memory Mapping →