sistemas-numericos-binario-hexadecimal

Number Systems: Binary and Hexadecimal in Electronics

  • 4 min

The binary system is a base-2 number system that represents information using only 0 and 1. Hexadecimal appears as its best ally because it allows us to write groups of bits in a compact and readable way when working with hardware.

This article is key because, even though we program at a high level today, when we go down to configure a microcontroller, we encounter bits, masks, and hexadecimal registers everywhere. If you don’t understand this, a datasheet becomes a hieroglyph.

Humans count in the decimal system (base 10) for a simple reason: we have 10 fingers. But machines don’t have fingers. They have transistors. And a transistor fundamentally works like a switch: it is either on (1) or off (0).

That’s why, to talk to processors, we need to learn their language. Today we’ll look at binary (the language of hardware) and hexadecimal (the language of programmers).

The Binary System (Physical Reality)

The binary system is a base-2 system. Only two symbols exist: 0 and 1.

Each binary digit is called a bit (binary digit).

  • Logic 0: 0 volts (GND).
  • Logic 1: 5 volts (or 3.3V).

Grouping Bits

Since a single bit provides little information, we group them:

  • Nibble: 4 bits (e.g., 1010).
  • Byte: 8 bits (e.g., 11001010). It is the standard storage unit.
  • Word: 16 or 32 bits, depending on the processor architecture.

Counting in Binary

It works the same as decimal, but it “overflows” much sooner.

  • Decimal: 0, 1, 2… 9 (symbols run out) 10.
  • Binary: 0, 1 (symbols run out) 10.
DecimalBinary (4 bits)
00000
10001
20010
30011
40100
50101

Each position to the left is worth double the previous one (powers of 2). 1011 = 1 · 8 + 0 · 4 + 1 · 2 + 1 · 1 = 11 in decimal.

The Hexadecimal System

Computers don’t “think” in hexadecimal. They think in binary. Hexadecimal is a system for humans.

Writing binary is error-prone. Memorizing 1111000010100101 is nearly impossible. To solve this, we use the hexadecimal (base 16) system. We use numbers from 0 to 9 and add the letters A, B, C, D, E, F.

The Key Correspondence

This is the reason for its existence. One hexadecimal digit represents exactly 4 bits (a nibble).

DecimalBinaryHexadecimal
0-90000-10010-9
101010A
111011B
121100C
131101D
141110E
151111F

Thanks to this, we can compress that horrible binary number from before:

  • Binary: 1111 0000 1010 0101
  • Hex: F 0 A 5

Writing 0xF0A5 is much cleaner and easier to remember.

Why is it Essential for Registers?

When we program microcontrollers at a low level, we don’t use generic functions. We manipulate registers.

A register is a row of switches (e.g., 8 bits) that controls hardware. Imagine the PORTD register, which controls digital pins of a microcontroller. If we want to turn on pins 0, 2, 4, and 6, and turn off the others, we need to send this pattern: 0 1 0 1 0 1 0 1 (Binary 01010101).

We can write it in code in three ways:

// 1. Decimal (hides which bits are active)
PORTD = 85; 

// 2. Binary (Visual, but long)
PORTD = 0b01010101; 

// 3. Hexadecimal (compact)
PORTD = 0x55;
Copied!

When you see professional code, you’ll see things like 0xFF (all ones, 11111111) or 0x00 (all zeros). Knowing how to mentally convert 0xA into 1010 is a skill that will save you hours of debugging.

Prefixes in Programming

So the compiler knows what we’re talking about:

  • 0bxxxx → Binary (e.g., 0b0011)
  • 0xxxxx → Hexadecimal (e.g., 0x3F)
  • No prefix → Decimal (e.g., 12)

Important Correspondences

  • Binary: Represents two logic states via voltage ranges.
  • Hexadecimal: A compact way to represent binary.
  • Key Correspondence: 1 Hex = 4 Bits.
  • F = 1111: The “all on” of 4 bits.
  • FF = 11111111: The “all on” of a Byte.

Understanding this is the necessary toll to enter digital logic. Now that we know how to represent 0s and 1s, let’s see what operations we can perform with them.