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
Each position represents a power of 2.
2.2 Positional Value
Example:
1011₂
Break it down:
1×2³ + 0×2² + 1×2¹ + 1×2⁰
= 8 + 0 + 2 + 1
= 11₁₀
2.3 Binary Table (Important to Memorize)
| Power | Value |
|---|---|
| 2⁰ | 1 |
| 2¹ | 2 |
| 2² | 4 |
| 2³ | 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
2.5 Binary Representation of Numbers
Positive Numbers
Stored directly:
5 → 00000101
Negative Numbers (Two’s Complement)
Steps:
- Convert to binary
- Invert bits
- Add 1
Example: -5 (8-bit)
5 = 00000101
Invert = 11111010
+1 = 11111011
2.6 Binary Operations
AND (&)
1 & 1 = 1
1 & 0 = 0
OR (|)
1 | 0 = 1
XOR (^)
1 ^ 1 = 0
1 ^ 0 = 1
NOT (~)
~00000101 = 11111010
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;
}
3. Hexadecimal Number System (Base 16)
3.1 Definition
Hexadecimal uses:
0–9 and A–F
| Hex | Decimal |
|---|---|
| A | 10 |
| B | 11 |
| C | 12 |
| D | 13 |
| E | 14 |
| F | 15 |
3.2 Why Hexadecimal?
Binary is long:
11111111
Hex makes it short:
FF
So:
1 byte = 8 bits = 2 hex digits
3.3 Positional Value
Example:
2F₁₆
2×16¹ + 15×16⁰
= 32 + 15
= 47₁₀
3.4 Binary ↔ Hex Conversion (CRITICAL SKILL)
Rule:
Group binary into 4 bits
Example 1: Binary → Hex
10101111
Group:
1010 1111
Convert:
1010 = A
1111 = F
Result:
AF₁₆
Example 2: Hex → Binary
3C
Convert each:
3 = 0011
C = 1100
Result:
00111100
3.5 Hex in Programming
C Example:
int x = 0xFF; // 255
int y = 0x1A; // 26
Memory Address:
0x7ffeefbff5c8
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
5.2 Bit Masking
int flags = 0x0F; // 00001111
5.3 Embedded Systems
Registers:
0xFF00 → hardware control
5.4 Networking
IP (hex view):
C0 A8 01 01 → 192.168.1.1
5.5 Assembly Language
MOV AX, 0x1F
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
6.2 Decimal → Hex
Divide by 16:
47 ÷ 16 = 2 r15 (F)
2 ÷ 16 = 0 r2
Result: 2F
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)
- Memorize powers of 2
- Memorize hex ↔ binary table
- Practice conversions daily
- Use C programs to verify results
- 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)