X Xerobit

Number Systems Guide — Binary, Octal, Decimal, Hexadecimal

Computers use binary, programmers use hexadecimal, and humans use decimal. Here's how all four number systems work, how to convert between them, and where each system is used...

Mian Ali Khalid · · 6 min read
Use the tool
Number Base Converter
Convert between binary, octal, decimal, hexadecimal, and text (UTF-8). Handles arbitrary lengths. Per-byte and per-character views.
Open Number Base Converter →

Computers store all data in binary (base 2). Programmers use hexadecimal (base 16) as a compact representation of binary. Octal (base 8) appears in Unix file permissions. Understanding all four systems and how they relate helps you read memory dumps, debug bit operations, and understand color codes.

Use the Number Base Converter to convert between binary, octal, decimal, and hex.

The four number systems

SystemBaseDigitsExample
Binary20–11010
Octal80–712
Decimal100–910
Hexadecimal160–9, A–FA

All four represent the same values — just in different bases. The decimal number 10 is the same quantity as binary 1010, octal 12, and hex A.

Positional value

Each digit represents a power of the base:

Decimal 432:

4 × 10² + 3 × 10¹ + 2 × 10⁰
= 4 × 100 + 3 × 10 + 2 × 1
= 400 + 30 + 2
= 432

Binary 1101:

1 × 2³ + 1 × 2² + 0 × 2¹ + 1 × 2⁰
= 1 × 8 + 1 × 4 + 0 × 2 + 1 × 1
= 8 + 4 + 0 + 1
= 13 (decimal)

Hex 2F:

2 × 16¹ + F × 16⁰
= 2 × 16 + 15 × 1
= 32 + 15
= 47 (decimal)

Converting between systems

Decimal to binary (divide by 2)

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: 101010 = 32 + 0 + 8 + 0 + 2 + 0 = 42 ✓

Binary to hexadecimal (group by 4)

Convert 11001111 to hex:

Split into 4-bit groups from right:
1100 1111

Convert each group:
1100 = 12 = C
1111 = 15 = F

Result: CF

Verify: CF = 12 × 16 + 15 = 192 + 15 = 207 Binary: 11001111 = 128 + 64 + 8 + 4 + 2 + 1 = 207 ✓

This is why hex is popular with programmers — each hex digit represents exactly 4 bits (one nibble), making conversion trivial.

Quick binary-to-hex lookup

0000 = 0    0100 = 4    1000 = 8    1100 = C
0001 = 1    0101 = 5    1001 = 9    1101 = D
0010 = 2    0110 = 6    1010 = A    1110 = E
0011 = 3    0111 = 7    1011 = B    1111 = F

Decimal to hex

Convert 255 to hex:

255 ÷ 16 = 15 remainder 15 (= F)
 15 ÷ 16 =  0 remainder 15 (= F)

Read bottom to top: FF

FF = 15 × 16 + 15 = 240 + 15 = 255 ✓

Where each system is used

Binary

  • CPU architecture (everything ultimately in binary)
  • Bitwise operations in code
  • Boolean logic and flags
  • Network subnet masks (11111111.11111111.11111111.00000000)
  • Floating-point representation (IEEE 754)

Octal

  • Unix file permissions: chmod 755 means rwxr-xr-x
    7 = 111 = rwx (all permissions)
    5 = 101 = r-x (read and execute)
    4 = 100 = r-- (read only)
  • C/Python integer literals: 0o755
  • Network protocols (historical)

Hexadecimal

  • Memory addresses: 0x7fff5fbff8b0
  • Color codes: #3B82F6 (R=3B, G=82, B=F6)
  • Hash values: 2cf24dba5fb0a30e...
  • UUID: f47ac10b-58cc-4372-a567-0e02b2c3d479
  • Bytecode and disassembly: 48 89 c7 e8 a1 00 00 00
  • ASCII/Unicode code points: U+0041 = ‘A’
  • IPv6 addresses: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Code: converting in different languages

JavaScript

// Decimal to other bases:
(255).toString(2);   // "11111111" (binary)
(255).toString(8);   // "377" (octal)
(255).toString(16);  // "ff" (hex)

// Other bases to decimal (parseInt with radix):
parseInt('11111111', 2);  // 255 (binary to decimal)
parseInt('377', 8);       // 255 (octal to decimal)
parseInt('ff', 16);       // 255 (hex to decimal)

// Hex with prefix:
parseInt('0xff', 16);  // 255
Number('0xff');        // 255

// Literal syntax:
const binary  = 0b11111111;  // 255
const octal   = 0o377;       // 255
const hex     = 0xff;        // 255
const decimal = 255;

Python

# Decimal to other bases:
bin(255)   # '0b11111111'
oct(255)   # '0o377'
hex(255)   # '0xff'

# Without prefix:
format(255, 'b')   # '11111111'
format(255, 'o')   # '377'
format(255, 'x')   # 'ff'
format(255, 'X')   # 'FF' (uppercase)
format(255, '08b') # '11111111' (padded to 8 digits)
format(255, '04x') # '00ff' (padded to 4 digits)

# Other bases to decimal:
int('11111111', 2)  # 255 (binary string)
int('377', 8)       # 255 (octal string)
int('ff', 16)       # 255 (hex string)
int('0xff', 16)     # 255 (with 0x prefix)

# Literal syntax:
binary  = 0b11111111  # 255
octal   = 0o377       # 255
hex_val = 0xFF        # 255

Bitwise operations

Understanding binary is essential for bitwise operations:

// AND: both bits must be 1
0b1100 & 0b1010  // 0b1000 = 8
//  1100
//  1010
//  ----
//  1000

// OR: either bit must be 1
0b1100 | 0b1010  // 0b1110 = 14

// XOR: exactly one bit must be 1
0b1100 ^ 0b1010  // 0b0110 = 6

// NOT (bitwise complement):
~0b1100  // -13 (two's complement)

// Left shift (multiply by 2^n):
1 << 3   // 8 (1 shifted left 3 positions)

// Right shift (divide by 2^n):
8 >> 1   // 4
8 >> 2   // 2

Related posts

Related tool

Number Base Converter

Convert between binary, octal, decimal, hexadecimal, and text (UTF-8). Handles arbitrary lengths. Per-byte and per-character views.

Written by Mian Ali Khalid. Part of the Data & Format pillar.