Binary, Hex, and Number Systems: The Foundation Every Developer Needs
Every color on your screen is a hex value. Every character you type is a binary sequence. Every IP address is four decimal numbers hiding an underlying binary representation. Understanding number systems isn't optional for developers — it's the foundation everything else rests on.
Key Takeaways
- Binary (base 2) is fundamental because transistors have two states: on and off
- Hexadecimal (base 16) is shorthand for binary — each hex digit represents exactly 4 bits
- Two's complement is how computers store negative integers — not with a minus sign
- Hex color #RRGGBB stores three bytes: red (00–FF), green (00–FF), blue (00–FF)
- Floating point (IEEE 754) is why 0.1 + 0.2 ≠ 0.3 in most programming languages
Why Binary? The Physics of Computing
Computers use binary not because of arbitrary choice but because of physics. Transistors — the billions of switches that make up a CPU — have two stable states: conducting current (1) or not conducting (0). A transistor trying to represent 10 different states would be unreliable and noise-prone. Two states are clean, fast, and manufacturable at nanometer scale.
Every number, character, image, sound file, and program instruction is ultimately stored as a sequence of 0s and 1s. A single binary digit is a bit. Eight bits make a byte, which can represent 256 different values (2⁸ = 256).
How to convert decimal to binary:
Convert 42 to binary:
42 ÷ 2 = 21 remainder 0
21 ÷ 2 = 10 remainder 1
10 ÷ 2 = 5 remainder 0
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
Read remainders bottom to top: 101010
Verify: 1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42 ✓
Hexadecimal: Binary Made Readable
Writing binary for any meaningful address or color value is unwieldy. The hex address 0x7FFE3D8A1C00 is far more readable than its 48-bit binary equivalent. Hexadecimal (base 16) uses digits 0–9 and letters A–F, where A=10, B=11, C=12, D=13, E=14, F=15.
The key insight: one hex digit represents exactly 4 bits (since 16 = 2⁴). So one byte (8 bits) is always two hex digits. This makes conversion between binary and hex trivial — just group the bits in fours.
Hex Color Codes Decoded
Two's Complement: How Computers Handle Negative Numbers
A computer doesn't have a "minus sign" in hardware. Negative numbers are stored using a technique called two's complement. In an 8-bit signed integer, the most significant bit (leftmost) serves as the sign bit: 0 means positive, 1 means negative.
To find the two's complement of a number: flip all bits (get the one's complement), then add 1. Example: -42 in 8-bit binary. 42 = 00101010. Flip all bits: 11010101. Add 1: 11010110. So -42 is stored as 11010110. This is why signed 8-bit integers range from -128 to +127 rather than -127 to +127.
The Floating Point Problem: Why 0.1 + 0.2 ≠ 0.3
IEEE 754 floating point standard represents numbers like scientific notation in binary: a sign bit, exponent bits, and mantissa bits. The problem is that fractions like 0.1 cannot be represented exactly in binary — just as 1/3 cannot be represented exactly in decimal (0.333...).
JavaScript / Python / Most Languages:
0.1 + 0.2
// 0.30000000000000004
0.1 + 0.2 === 0.3
// false
Fix: Use integer arithmetic (store cents, not dollars) or a decimal library for financial calculations.
Number System Quick Reference
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 8 | 1000 | 10 | 8 |
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 16 | 00010000 | 20 | 10 |
| 255 | 11111111 | 377 | FF |
Convert Numbers Instantly
Binary / Hex Converter
Convert between decimal, binary, hexadecimal, and octal instantly. View step-by-step conversion breakdowns and bit-level representations.
Open Number Converter →