Binary, octal, hex: why each base shows up in programming

3 min read

Decimal feels natural to humans, but binary, octal, and hex all show up regularly in code. This article walks through why each base exists and when it’s the right tool.

Notations across bases

BaseNameDigitsExample (decimal 255)Prefix
2binary0, 1111111110b
8octal0–73770o or 0
10decimal0–9255(none)
16hexadecimal0–9, A–FFF0x

In JavaScript, 0b11111111, 0o377, 255, and 0xff all equal the same number.

Why these bases match bits

Bases that are powers of 2 align cleanly with bits:

  • Binary: 1 digit = 1 bit
  • Octal: 1 digit = 3 bits (2³)
  • Hex: 1 digit = 4 bits (2⁴)

Decimal isn’t a power of 2, so converting 255 ↔ 11111111 is mental math. Hex gives you a 1-to-4-bit shortcut: 0xFF = 1111 1111, no calculation required. Hex is the compact representation of bit strings.

Why hex won over octal

Both align with binary, but hex dominates today because:

  • One byte = two hex digits (00 to FF).
  • Word sizes line up: 32 bits = 8 hex digits, 64 bits = 16.
  • Byte boundaries are visible, unlike 3-digit octal groupings.

Octal lingers in places where 3-bit groupings are natural (Unix chmod 755), but new code uses hex.

Conversion shortcuts

Binary → hex

Group bits into 4 from the right, map each group to a hex digit:

1111 1111 → F F → FF
1010 1100 → A C → AC

The reverse is the same in the opposite direction.

Decimal → binary

Repeatedly divide by 2, recording remainders bottom-up:

13 ÷ 2 = 6 r 1
 6 ÷ 2 = 3 r 0
 3 ÷ 2 = 1 r 1
 1 ÷ 2 = 0 r 1
→ 1101

Or “subtract powers of 2”: 13 = 8 + 4 + 1 = 1101.

Decimal → hex

Same divide-by-16 routine, or convert via binary in 4-bit groups.

Where hex appears in real code

  • Color codes: #FF8C42 — three 8-bit RGB channels in 2-digit hex.
  • Memory addresses: 0x7ffe0a3f...
  • Binary dumps: hexdump, xxd.
  • Hash digests: SHA-256 as 64 hex characters.
  • UTF-8 percent-encoding: %E3%81%82 = bytes E3, 81, 82.
  • MAC addresses: 00:1A:2B:3C:4D:5E.

Where octal still appears

  • chmod permissions: chmod 755 packs read/write/execute bits into octal digits.
  • C string escapes: \033 is ESC.
  • Linux umask.

For new code, you rarely have a reason to pick octal.

Language syntax

JavaScript:  0b1111, 0o17, 0xF
Python:      0b1111, 0o17, 0xF
Ruby:        0b1111, 0o17, 0xF
C:           0b1111 (C23+), 017, 0xF
Java:        0b1111, 017, 0xF
Rust:        0b1111, 0o17, 0xF

The legacy C convention of “leading zero means octal” (017 = 15) is error-prone, and modern languages prefer the explicit 0o prefix.

Summary

  • Binary and hex map directly to bits, so they replace decimal in low-level work.
  • Hex wins for readability — one byte per two digits, clean word boundaries.
  • Octal survives in chmod and a few older corners.
  • Conversion between binary and hex is a 4-bit-per-digit lookup.

To convert across bases quickly, the number base tool on this site handles binary, octal, decimal, and hex side by side.