DEV Community

Michael Lip
Michael Lip

Posted on • Originally published at zovo.one

Every Developer Needs to Think in at Least Three Number Bases

Decimal is for humans. Binary is for hardware. Hexadecimal is for developers who need to read binary without going blind. Understanding base conversion is not academic. It is practical knowledge you use every time you work with colors, memory addresses, or bitwise operations.

The three bases you need

Decimal (base 10): How humans count. Digits 0-9.

Binary (base 2): How computers store data. Digits 0 and 1.

Hexadecimal (base 16): How developers read binary efficiently. Digits 0-9 and A-F.

The relationship: every hex digit maps to exactly 4 binary digits. This is why hex is useful: it is a compact representation of binary.

Hex  Binary  Decimal
0    0000    0
1    0001    1
...
9    1001    9
A    1010    10
B    1011    11
C    1100    12
D    1101    13
E    1110    14
F    1111    15
Enter fullscreen mode Exit fullscreen mode

Converting by hand

Binary to hex: Group binary digits in fours from the right and convert each group.

Binary:  1101 0110 1010
Hex:     D    6    A
Result:  0xD6A
Enter fullscreen mode Exit fullscreen mode

Hex to binary: Replace each hex digit with its 4-bit binary equivalent.

Hex:    3F
Binary: 0011 1111
Enter fullscreen mode Exit fullscreen mode

Decimal to binary: Repeatedly divide by 2, record remainders.

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

Binary: 101010
Enter fullscreen mode Exit fullscreen mode

In JavaScript

const num = 42;
num.toString(2);   // "101010" (binary)
num.toString(8);   // "52" (octal)
num.toString(16);  // "2a" (hex)

parseInt("101010", 2);  // 42
parseInt("52", 8);      // 42
parseInt("2a", 16);     // 42
Enter fullscreen mode Exit fullscreen mode

Where this matters

CSS colors: #FF6600 is three hex values: R=FF (255), G=66 (102), B=00 (0).

File permissions: chmod 755 is octal for rwxr-xr-x in binary 111 101 101.

Memory addresses: Debuggers show addresses in hex because a 64-bit address in hex is 16 characters. In binary it would be 64 characters.

Network masks: A subnet mask of 255.255.255.0 is 11111111.11111111.11111111.00000000 in binary, which is /24 in CIDR notation.

For converting between any number base instantly, I keep a converter at zovo.one/free-tools/number-base-converter. It supports bases 2 through 36 with real-time conversion as you type.


I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.

Top comments (0)