DEV Community

Cover image for Binary and Hexadecimal Number Systems — A Complete Guide for Low-Level Programming
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on

Binary and Hexadecimal Number Systems — A Complete Guide for Low-Level Programming

When you work close to hardware—whether in C, C++, embedded systems, operating systems, or reverse engineering—you must think like the machine. And machines do not understand decimal numbers. They operate using binary (base-2). To make binary manageable for humans, we use hexadecimal (base-16).

This article gives you a complete, step-by-step understanding of both systems, with practical insight for real programming scenarios.


1. Why Number Systems Matter in Low-Level Programming

At the hardware level:

  • Memory stores bits (0 and 1)
  • CPU processes binary instructions
  • Registers, pointers, addresses → all binary

But binary is long and hard to read. That’s where hexadecimal comes in—it’s a compact human-friendly representation of binary.


2. Binary Number System (Base 2)

2.1 Definition

Binary uses only two digits:

0 and 1
Enter fullscreen mode Exit fullscreen mode

Each position represents a power of 2.


2.2 Positional Value

Example:

1011₂
Enter fullscreen mode Exit fullscreen mode

Break it down:

1×2³ + 0×2² + 1×2¹ + 1×2⁰
= 8 + 0 + 2 + 1
= 11₁₀
Enter fullscreen mode Exit fullscreen mode

2.3 Binary Table (Important to Memorize)

Power Value
2⁰ 1
2
4
8
2⁴ 16
2⁵ 32
2⁶ 64
2⁷ 128

These are critical for bit manipulation.


2.4 Binary in Memory

A bit = 0 or 1
A byte = 8 bits

Example:

00001010 → 10 in decimal
Enter fullscreen mode Exit fullscreen mode

2.5 Binary Representation of Numbers

Positive Numbers

Stored directly:

5 → 00000101
Enter fullscreen mode Exit fullscreen mode

Negative Numbers (Two’s Complement)

Steps:

  1. Convert to binary
  2. Invert bits
  3. Add 1

Example: -5 (8-bit)

5      = 00000101
Invert = 11111010
+1     = 11111011
Enter fullscreen mode Exit fullscreen mode

2.6 Binary Operations

AND (&)

1 & 1 = 1
1 & 0 = 0
Enter fullscreen mode Exit fullscreen mode

OR (|)

1 | 0 = 1
Enter fullscreen mode Exit fullscreen mode

XOR (^)

1 ^ 1 = 0
1 ^ 0 = 1
Enter fullscreen mode Exit fullscreen mode

NOT (~)

~00000101 = 11111010
Enter fullscreen mode Exit fullscreen mode

2.7 Example in C

#include <stdio.h>

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

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

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

3. Hexadecimal Number System (Base 16)

3.1 Definition

Hexadecimal uses:

0–9 and A–F
Enter fullscreen mode Exit fullscreen mode
Hex Decimal
A 10
B 11
C 12
D 13
E 14
F 15

3.2 Why Hexadecimal?

Binary is long:

11111111
Enter fullscreen mode Exit fullscreen mode

Hex makes it short:

FF
Enter fullscreen mode Exit fullscreen mode

So:

1 byte = 8 bits = 2 hex digits
Enter fullscreen mode Exit fullscreen mode

3.3 Positional Value

Example:

2F₁₆
Enter fullscreen mode Exit fullscreen mode
2×16¹ + 15×16⁰
= 32 + 15
= 47₁₀
Enter fullscreen mode Exit fullscreen mode

3.4 Binary ↔ Hex Conversion (CRITICAL SKILL)

Rule:

Group binary into 4 bits


Example 1: Binary → Hex

10101111
Enter fullscreen mode Exit fullscreen mode

Group:

1010 1111
Enter fullscreen mode Exit fullscreen mode

Convert:

1010 = A
1111 = F
Enter fullscreen mode Exit fullscreen mode

Result:

AF₁₆
Enter fullscreen mode Exit fullscreen mode

Example 2: Hex → Binary

3C
Enter fullscreen mode Exit fullscreen mode

Convert each:

3 = 0011
C = 1100
Enter fullscreen mode Exit fullscreen mode

Result:

00111100
Enter fullscreen mode Exit fullscreen mode

3.5 Hex in Programming

C Example:

int x = 0xFF;  // 255
int y = 0x1A;  // 26
Enter fullscreen mode Exit fullscreen mode

Memory Address:

0x7ffeefbff5c8
Enter fullscreen mode Exit fullscreen mode

4. Binary vs Hexadecimal (Comparison)

Feature Binary Hexadecimal
Base 2 16
Digits 0,1 0–9, A–F
Length Long Short
Usage Machine level Human-friendly

5. Real Use Cases in Low-Level Programming

5.1 Memory Inspection

Debugger output:

0x7fff  address
Enter fullscreen mode Exit fullscreen mode

5.2 Bit Masking

int flags = 0x0F; // 00001111
Enter fullscreen mode Exit fullscreen mode

5.3 Embedded Systems

Registers:

0xFF00  hardware control
Enter fullscreen mode Exit fullscreen mode

5.4 Networking

IP (hex view):

C0 A8 01 01 → 192.168.1.1
Enter fullscreen mode Exit fullscreen mode

5.5 Assembly Language

MOV AX, 0x1F
Enter fullscreen mode Exit fullscreen mode

6. Conversion Methods (Step-by-Step)

6.1 Decimal → Binary

Divide by 2 repeatedly:

13 ÷ 2 = 6 r1
6 ÷ 2  = 3 r0
3 ÷ 2  = 1 r1
1 ÷ 2  = 0 r1

Result: 1101
Enter fullscreen mode Exit fullscreen mode

6.2 Decimal → Hex

Divide by 16:

47 ÷ 16 = 2 r15 (F)
2 ÷ 16  = 0 r2

Result: 2F
Enter fullscreen mode Exit fullscreen mode

6.3 Binary → Decimal

Multiply powers of 2.


6.4 Hex → Decimal

Multiply powers of 16.


7. Important Patterns to Master

7.1 Hex ↔ Binary Table (Must Memorize)

Hex Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

8. Common Mistakes

  • Mixing base systems (e.g., treating hex as decimal)
  • Forgetting grouping in binary → hex
  • Ignoring leading zeros
  • Confusing ASCII vs numeric values

9. How to Master This (Practical Strategy)

  1. Memorize powers of 2
  2. Memorize hex ↔ binary table
  3. Practice conversions daily
  4. Use C programs to verify results
  5. Read memory using debuggers (gdb)

10. Final Insight

Binary is the language of machines.
Hexadecimal is the language of programmers working close to machines.

If you master both:

  • Bit manipulation becomes easy
  • Debugging becomes powerful
  • System programming becomes natural

Top comments (0)