Back to Blog

Binary, Hex, and Octal: Number Systems Every Programmer Should Know

NumberConvert Team8 min read

A comprehensive guide to binary, hexadecimal, and octal number systems. Learn why computers use these bases, how to convert between them, and their practical applications in programming.

Listen to this article

Browser text-to-speech

Introduction to Number Systems

Every number you encounter daily uses the decimal system—base 10. You learned to count with ten fingers, so base 10 feels natural. But computers do not have fingers. They have transistors that are either on or off, which means they naturally work in base 2: binary.

Understanding binary, hexadecimal (base 16), and octal (base 8) is essential for programmers. These number systems appear everywhere in computing—from memory addresses to color codes, file permissions to bitwise operations.

Why Computers Use Binary (Base 2)

The Hardware Reality

At the lowest level, computers are built from billions of transistors. Each transistor can exist in one of two states: on (conducting electricity) or off (not conducting). This binary state maps perfectly to the digits 0 and 1.

Why not use 10 different voltage levels for base 10?

Theoretically possible, but practically problematic:

  • Distinguishing between 10 voltage levels requires precise measurements
  • Electrical noise could cause errors (is that voltage 4 or 5?)
  • Binary is more reliable: any voltage above a threshold is 1, below is 0

Binary Representation

In binary, each digit position represents a power of 2:

Position76543210
Value1286432168421

For example, the binary number 10110101 equals:

  • 1×128 + 0×64 + 1×32 + 1×16 + 0×8 + 1×4 + 0×2 + 1×1
  • = 128 + 32 + 16 + 4 + 1
  • = 181 in decimal

Bits and Bytes

A single binary digit is called a bit. Eight bits form a byte, which can represent values from 0 to 255 (2^8 - 1). Common groupings include:

  • Nibble: 4 bits (0-15)
  • Byte: 8 bits (0-255)
  • Word: 16, 32, or 64 bits depending on processor architecture

Hexadecimal (Base 16): The Programmer's Favorite

Why Hexadecimal?

Binary is perfect for computers but terrible for humans. Reading 11111111101011001010111011110000 is exhausting and error-prone. Hexadecimal solves this by providing a compact representation.

Since 16 = 2^4, each hexadecimal digit represents exactly 4 binary digits (one nibble). This creates a perfect mapping:

DecimalBinaryHex
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F

Hexadecimal Notation

Hex numbers are commonly prefixed to distinguish them from decimal:

  • 0x prefix: 0xFF (C, JavaScript, Python)
  • # prefix: #FF5733 (CSS colors)
  • h suffix: FFh (Assembly language)

Common Hex Use Cases

1. Memory Addresses

Memory addresses are almost always displayed in hex:

0x00007FFD4A2B3C40

This 64-bit address is much more readable than its binary equivalent (64 ones and zeros).

2. Color Codes

Web colors use hex RGB values:

  • #FF0000 = Red (255 red, 0 green, 0 blue)
  • #00FF00 = Green
  • #0000FF = Blue
  • #FFFFFF = White (all channels at maximum)
  • #000000 = Black (all channels at zero)

Each pair of hex digits represents one color channel (0-255).

3. Byte Values

A single byte ranges from 00 to FF in hex (0-255 decimal). Two hex characters are easier to read than up to three decimal digits.

Octal (Base 8): The Historical System

Origins of Octal

Octal was popular in early computing for the same reason as hex—it maps cleanly to binary (8 = 2^3, so each octal digit represents 3 bits). Early computers like the PDP-8 used 12-bit words, which divide evenly by 3 but not by 4, making octal the natural choice.

Octal in Modern Computing

While mostly superseded by hex, octal survives in one important area: Unix file permissions.

chmod 755 script.sh

Each digit represents permissions for owner, group, and others:

  • 7 = 111 binary = read + write + execute
  • 5 = 101 binary = read + execute
  • 0 = 000 binary = no permissions

The permission bits:

OctalBinaryMeaning
0000No permission
1001Execute only
2010Write only
3011Write + Execute
4100Read only
5101Read + Execute
6110Read + Write
7111Full permission

Octal Notation

Octal numbers typically use:

  • 0 prefix: 0755 (C, JavaScript)
  • 0o prefix: 0o755 (Python 3)

Converting Between Number Systems

Decimal to Binary

Method: Repeated Division by 2

To convert 181 to binary:

  1. 181 ÷ 2 = 90 remainder 1
  2. 90 ÷ 2 = 45 remainder 0
  3. 45 ÷ 2 = 22 remainder 1
  4. 22 ÷ 2 = 11 remainder 0
  5. 11 ÷ 2 = 5 remainder 1
  6. 5 ÷ 2 = 2 remainder 1
  7. 2 ÷ 2 = 1 remainder 0
  8. 1 ÷ 2 = 0 remainder 1

Reading remainders bottom-to-top: 10110101

Binary to Hexadecimal

Method: Group into Nibbles

Convert 10110101 to hex:

  1. Group into 4-bit sections from right: 1011 0101
  2. Convert each group: 1011 = B, 0101 = 5
  3. Result: B5

Hexadecimal to Decimal

Method: Positional Notation

Convert B5 to decimal:

  1. B = 11, position 1: 11 × 16^1 = 176
  2. 5 = 5, position 0: 5 × 16^0 = 5
  3. Total: 176 + 5 = 181

Quick Reference Conversions Table

DecimalBinaryOctalHex
0000
81000108
10101012A
15111117F
16100002010
321000004020
64100000010040
100110010014464
12711111111777F
1281000000020080
25511111111377FF
256100000000400100

Binary Arithmetic Basics

Binary Addition

Binary addition follows simple rules:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 10 (0, carry 1)

Example: 1011 + 0110

    1011  (11 in decimal)
  + 0110  (6 in decimal)
  ------
   10001  (17 in decimal)

Binary Subtraction

Using two's complement (how computers actually subtract):

  1. Invert all bits of the subtrahend
  2. Add 1
  3. Add to the minuend

Example: 1011 - 0110 (11 - 6)

  1. Invert 0110 → 1001
  2. Add 1 → 1010
  3. Add: 1011 + 1010 = 10101
  4. Discard overflow: 0101 = 5

Bitwise Operations

Programmers frequently use bitwise operations:

AND (&): Both bits must be 1

1010 & 1100 = 1000

OR (|): Either bit can be 1

1010 | 1100 = 1110

XOR (^): Exactly one bit must be 1

1010 ^ 1100 = 0110

NOT (~): Flip all bits

~1010 = 0101

Left Shift (<<): Multiply by 2

1010 << 1 = 10100

Right Shift (>>): Divide by 2

1010 >> 1 = 0101

Practical Applications

Debugging Memory

When debugging, memory dumps display hex:

0x0000: 48 65 6C 6C 6F 20 57 6F  Hello Wo
0x0008: 72 6C 64 21 00 00 00 00  rld!....

Each byte shown in hex, with ASCII interpretation on the right.

Network Protocols

MAC addresses use hex: 00:1A:2B:3C:4D:5E IPv6 addresses use hex: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Cryptography

Hash values are displayed in hex:

SHA-256: e3b0c44298fc1c149afbf4c8996fb924...

Assembly and Low-Level Programming

Instruction opcodes, register values, and memory addresses all use hex for readability.

Key Takeaways

  1. Binary is the native language of computers because transistors have two states
  2. Hexadecimal provides human-readable binary with a perfect 4:1 bit mapping
  3. Octal survives primarily in Unix permissions due to the 3-bit grouping
  4. Converting between bases becomes intuitive with practice
  5. Bitwise operations are powerful tools for efficient programming

See what our calculators can do for you

Ready to take control of your finances?

Use our free converters to switch between binary, hex, decimal, and octal

Explore Our Tools

Frequently Asked Questions

Common questions about the Binary, Hex, and Octal: Number Systems Every Programmer Should Know

Computers use binary because their fundamental building blocks—transistors—have two states: on and off. Using only two voltage levels (high for 1, low for 0) makes circuits more reliable and less susceptible to electrical noise than trying to distinguish between 10 different voltage levels for decimal.