Number Bases: Binary, Hex,Octal & Decimal Explained

Four number bases appear throughout programming — from bit flags to CSS hex codes to Unix permissions. Learn how each works, where it's used, and how to convert between them.

Why Developers Deal with Multiple Number Bases

Humans count in base 10 because we evolved with ten fingers. Computers use base 2 because transistors have exactly two states. And somewhere between human intuition and machine reality, engineers invented base 8 and base 16 as compact, human-readable shorthand for binary. Every developer encounters all four number bases — binary when debugging bit flags and reading subnet masks, hexadecimal when working with CSS colors and memory addresses, octal when setting Unix file permissions, and decimal for everything else. Understanding how these bases relate turns a cryptic hex dump from intimidating to readable.

The Four Bases and Where They Appear

BaseNameDigitsWhere You'll See It
2Binary0, 1Bit flags, bitwise operations, network masks, FPGA programming, file format headers
8Octal0-7Unix file permissions (chmod), legacy C escape sequences, some aviation transponder codes
10Decimal0-9Everyday numbers, user interfaces, most programming language literals by default
16Hexadecimal0-9, A-FCSS colors, memory addresses, binary data representation (hex dumps), MAC addresses, Unicode code points

How Positional Notation Works

Every base uses the same mechanism. Each digit position represents a power of the base, counted from right to left starting at zero. The value of the number is the sum of each digit multiplied by its place value. The number 255 in decimal: 2 × 10² + 5 × 10¹ + 5 × 10⁰ = 200 + 50 + 5. In binary, 11111111: 1×2⁷ + 1×2⁶ + 1×2⁵ + 1×2⁴ + 1×2³ + 1×2² + 1×2¹ + 1×2⁰ = 128+64+32+16+8+4+2+1 = 255. In hexadecimal, FF: F(15) × 16¹ + F(15) × 16⁰ = 240 + 15 = 255. Same number — 255 — rendered three different ways. The notation changes; the value doesn't.

This equivalence is why hex is used as binary shorthand. One hex digit represents exactly four binary digits (a nibble). Two hex digits represent exactly eight binary digits (a byte). Reading FF is faster than reading 11111111, but they describe the same byte. A number base converter switches between them instantly.

Binary in Practice: Bitwise Operations

// File permissions as bit flags
const READ   = 0b001;  // decimal 1
const WRITE  = 0b010;  // decimal 2
const EXEC   = 0b100;  // decimal 4

let perms = READ | WRITE;             // 0b011 = 3 (bitwise OR)
let canRead = (perms & READ) !== 0;   // true   (bitwise AND)
let canExec = (perms & EXEC) !== 0;   // false  (bitwise AND)
perms |= EXEC;                        // 0b111 = 7 (set the EXEC bit)
perms &= ~WRITE;                      // 0b101 = 5 (clear the WRITE bit)
perms ^= READ;                        // 0b100 = 4 (toggle the READ bit)

Bit flags pack multiple boolean values into a single integer. Each bit position represents a separate on/off state. This technique appears in file permissions, network protocol headers, hardware registers, and anywhere memory efficiency matters. The bitwise operators (|, &, ^, ~, <<, >>) manipulate individual bits directly — they're fast, but the code is harder to read than named booleans. Use bit flags when you need the compactness; use objects with named properties when readability matters more.

Hexadecimal in Practice: Colors and Bytes

/* Hex colors: each byte is a channel (RRGGBB) */
color: #3B82F6;     /* R=59, G=130, B=246 */
color: #FF0000;     /* Pure red: R=255, G=0, B=0 */
color: #FFFFFF;     /* White: all channels maxed */
color: #000000;     /* Black: all channels zero */
color: #38f;         /* Shorthand: #3388ff — each digit doubled */

Every pair of hex digits in a CSS color is one byte (8 bits, 0-255). RR for red, GG for green, BB for blue. Understanding this makes color manipulation intuitive: to make a color darker, decrease each byte; to shift toward blue, increase the BB byte; to make it transparent, add an alpha byte (RRGGBBAA).

# Memory addresses are conventionally shown in hex
0x7fff5fbff000    # Stack address (typical for macOS/Linux)
0xDEADBEEF        # "Dead beef" — a magic number used by debuggers
0xCAFEBABE        # "Cafe babe" — Java class file magic number

Octal in Practice: chmod

chmod 755 script.sh    # rwxr-xr-x  owner: all, group: read+execute, others: read+execute
chmod 644 config.json  # rw-r--r--  owner: read+write, group+others: read only
chmod 600 secrets.env  # rw-------  owner only

# Octal digit = 3 bits: 4=read, 2=write, 1=execute. Sum to combine:
# 7 = 4+2+1 = rwx    (read, write, execute)
# 6 = 4+2+0 = rw-    (read, write)
# 5 = 4+0+1 = r-x    (read, execute)
# 4 = 4+0+0 = r--    (read only)
# 0 = 0+0+0 = ---    (no permissions)

# Three digits: owner | group | others
# chmod 755 = owner:7(rwx) group:5(r-x) others:5(r-x)

Converting Between Bases Mentally

Binary to hex: group binary digits into chunks of 4 and convert each chunk. 1111 1111F FFF. 0011 10113 B3B. Hex to binary: expand each hex digit into 4 bits. 3B0011 1011. Binary to octal: group into chunks of 3. 111 111 1117 7 7777.

Hex to decimal: for quick estimates, remember the landmarks. 0xFF = 255. 0x100 = 256. 0x1000 = 4096. 0xFFFF = 65535. For exact conversions, use a converter — mental arithmetic for arbitrary hex-to-decimal is error-prone past two digits.

BigInt for Large Numbers

JavaScript's Number type is a 64-bit float that safely represents integers up to 2⁵³ - 1 (about 9 quadrillion). For larger values — 256-bit cryptographic hashes, blockchain transaction amounts, astronomical-scale identifiers — use BigInt (available since ES2020). Our Number Base converter uses BigInt internally, handling arbitrarily large integers across binary, octal, decimal, and hex without precision loss or overflow.

Quick Reference

DecimalBinaryOctalHex
10101012A
15111117F
16100002010
25511111111377FF

Number Bases in Protocols and File Formats

Network byte order is essentially a base-256 positional system. IPv4 subnet masks are binary patterns. Unicode code points are conventionally hex: U+0041 is A, U+1F600 is the grinning face emoji. ASCII codes are documented in decimal, hex, and octal. The more systems you work with, the more number bases you will encounter — they are the shared vocabulary between human-readable representation and machine-readable data.