LESSONEmbedded C · Part 8EP.202 · 24 s

Embedded C Part 8: the bit shift trick — what does 1 << 5 mean?

// WHAT YOU WILL LEARN

1 << n makes a mask with only bit n set (1<<5 = 0b00100000).

// LESSON NOTES

What does 1 << 5 mean in C? 🤔💻

1️⃣ Start with a 1 in bit 0: 0b00000001.
⬅️ << 5 shifts it LEFT five places — and every step doubles it: 1 → 2 → 4 → 8 → 16 → 32.
✅ So 1 << 5 = 0b00100000 = 0x20 = 32.
💡 On an Arduino Uno, bit 5 of port B (PB5) is pin 13 — the LED!
🎭 It's a MASK: a byte with only one bit on, so you can aim at exactly one pin.

DDRB = (1 << 5); // PB5 (pin 13) → output

TESTED CODE · ec08_bit_shift.c · avr-gcc
// EC L08 - 1 << n builds a mask with only bit n set
#include <avr/io.h>

int main(void) {
    uint8_t m0 = (1 << 0);   // 0b00000001
    uint8_t m3 = (1 << 3);   // 0b00001000
    uint8_t m5 = (1 << 5);   // 0b00100000 = 0x20
    DDRB = m5; (void)m0; (void)m3;
    while (1) { }
}
Read the voiceover (transcript)
  1. What does one shift-left five mean?
  2. Start with a one in bit zero.
  3. Shift it left five places. Each step doubles it!
  4. One, two, four, eight, sixteen, thirty-two!
  5. So which pin does it pick?
  6. Bit five: pin thirteen, the LED!
  7. A mask with only one bit on.
  8. Next: set one bit without breaking the rest. Follow for Part 9!

Related lessons