LESSONEmbedded C · Part 4EP.55 · 24 s

Binary & hex: one byte, three ways

// WHAT YOU WILL LEARN

One byte = 8 bits; 0b00100000 = 0x20 = 32 are the same number.

// LESSON NOTES

0b00100000, 0x20 and 32… three numbers, or one byte? 🔢

1️⃣ BINARY (base 2): every bit has a place value: 128 64 32 16 8 4 2 1. Bit 5 on = 32. Bits 5, 2 and 0 = 32 + 4 + 1 = 37. "0b" tells C it's binary.

2️⃣ HEX (base 16): split the byte into two groups of 4 bits. Each group is one hex digit, 0–9 then A–F. 0010 0000 → 2 0 → 0x20. All ones: 1111 1111 = 0xFF = 255.

3️⃣ IN YOUR C CODE: DDRB = 0b00100000; DDRB = 0x20; DDRB = 32; all three set the exact same 8 bits (bit 5 → pin D13 becomes an output).

📌 Binary, hex, decimal: just three ways to write the same byte.

TESTED CODE · ec04_binary_hex.c · avr-gcc
// EC L04 - one byte, three ways to write it
#include <avr/io.h>

int main(void) {
    DDRB = 0b00100000;   // binary : bit 5 = 1
    DDRB = 0x20;         // hex    : same byte
    DDRB = 32;           // decimal: same byte
    while (1) { }
}
Read the voiceover (transcript)
  1. Three numbers... or one byte?
  2. Binary: every bit has a place value. Bit five is thirty-two!
  3. Hex packs four bits into one digit, so this byte is hex two zero. All ones? F F, two fifty-five!
  4. In C, all three lines set the exact same bits!
  5. Binary, hex, decimal: same byte!
  6. Next: hash define! Follow for part five!

Related lessons