Back to Blog
Technicalbinaryhexadecimaloctal

Number Systems: Understanding Binary, Hexadecimal, and Octal

Master binary, hex, and octal number systems. Learn conversions, bitwise operations, Unix permissions, and why programmers use these bases daily.

Loopaloo TeamAugust 28, 202515 min read

Every number you see on a screen is stored as binary in your computer's memory. Understanding binary, hexadecimal, and octal helps you work with low-level code, debug memory issues, and understand how computers actually work.

The Basics: What is a Number System?

A number system (or base) defines how we represent values using digits. The base tells you how many unique digits are available.

SystemBaseDigits UsedExample
Binary20, 11010
Octal80-712
Decimal100-910
Hexadecimal160-9, A-FA

All of these represent the same value: ten in decimal.

Binary (Base-2): The Language of Computers

Binary uses only two digits: 0 and 1. Each digit is called a bit (binary digit). This matches how computers work - transistors are either on (1) or off (0).

How Binary Counting Works

In decimal, each position is worth 10× the position to its right. In binary, each position is worth 2× the position to its right.

Position values (right to left):
... 128  64  32  16   8   4   2   1
      2⁷  2⁶  2⁵  2⁴  2³  2²  2¹  2⁰

Converting Binary to Decimal

Add up the position values where there's a 1:

Binary: 1 0 1 1 0 1 0 1
        │ │ │ │ │ │ │ └─ 1 × 1   = 1
        │ │ │ │ │ │ └─── 0 × 2   = 0
        │ │ │ │ │ └───── 1 × 4   = 4
        │ │ │ │ └─────── 0 × 8   = 0
        │ │ │ └───────── 1 × 16  = 16
        │ │ └─────────── 1 × 32  = 32
        │ └───────────── 0 × 64  = 0
        └─────────────── 1 × 128 = 128
                                  ─────
                         Total:   181

So 10110101 in binary = 181 in decimal.

Converting Decimal to Binary

Repeatedly divide by 2 and track the remainders:

Convert 45 to binary:
45 ÷ 2 = 22 remainder 1  ↑
22 ÷ 2 = 11 remainder 0  │
11 ÷ 2 = 5  remainder 1  │  Read upward
5  ÷ 2 = 2  remainder 1  │
2  ÷ 2 = 1  remainder 0  │
1  ÷ 2 = 0  remainder 1  │

Result: 101101

Common Binary Values

DecimalBinaryNotes
00Zero
11One
210First "carry"
71113 bits max
810004 bits needed
1511114 bits max (nibble)
16100005 bits needed
255111111118 bits max (byte)
2561000000009 bits needed

Binary Arithmetic

Addition:         Subtraction:
  1011 (11)         1011 (11)
+ 0101 (5)        - 0101 (5)
──────            ──────
 10000 (16)         0110 (6)

Hexadecimal (Base-16): Compact Binary

Hexadecimal (hex) uses 16 digits. Since we only have 10 numeric digits (0-9), we use letters A-F for values 10-15.

DecimalHexBinary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
10A1010
11B1011
12C1100
13D1101
14E1110
15F1111

Why Hex is Perfect for Binary

One hex digit represents exactly 4 binary digits (a nibble). This makes conversion trivial:

Binary:  1111 1010 0011 1100
Hex:       F    A    3    C

So 1111101000111100 = 0xFA3C

This is why hex is everywhere in computing - it's a human-readable shorthand for binary.

Converting Hex to Decimal

Each position is worth 16× the position to its right:

Hex: 2 A F
     │ │ └─ F (15) × 1   = 15
     │ └─── A (10) × 16  = 160
     └───── 2      × 256 = 512
                          ─────
                   Total: 687

Common Uses for Hexadecimal

Colors (CSS/HTML):

color: #FF5733;     /* RGB: 255, 87, 51 */
color: #FF5733CC;   /* With alpha: 80% opacity */

Each pair represents a color channel (00-FF = 0-255):

#FF5733
 ││││││
 ││││└┴─ Blue:  0x33 = 51
 ││└┴─── Green: 0x57 = 87
 └┴───── Red:   0xFF = 255

Memory Addresses:

0x7FFF5FBFF8C0  (Stack address)
0x00007FF6     (Kernel address)

Character Codes:

'A' = 0x41 = 65
'a' = 0x61 = 97
' ' = 0x20 = 32

MAC Addresses:

00:1A:2B:3C:4D:5E

Octal (Base-8): Unix Permissions

Octal uses digits 0-7. One octal digit represents exactly 3 binary digits.

Binary: 111 101 101
Octal:   7   5   5

So 111101101 = 755 in octal

Unix File Permissions

This is where you'll encounter octal most often:

chmod 755 script.sh

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

Permission bits:
  r (read)    = 4 = 100
  w (write)   = 2 = 010
  x (execute) = 1 = 001

Example: 755
  7 = rwx (4+2+1) = owner can read, write, execute
  5 = r-x (4+0+1) = group can read and execute
  5 = r-x (4+0+1) = others can read and execute

Common Permission Values

OctalBinaryMeaningSymbolic
0000No permissions---
1001Execute only--x
4100Read onlyr--
5101Read + executer-x
6110Read + writerw-
7111Full permissionsrwx
ModeUse Case
755Executable scripts
644Regular files
700Private directories
777Open to everyone (avoid!)

Number Prefixes in Code

Different languages use prefixes to indicate the base:

// JavaScript
let binary = 0b1010;      // 10 in decimal
let octal = 0o755;        // 493 in decimal
let hex = 0xFF;           // 255 in decimal
# Python
binary = 0b1010           # 10
octal = 0o755             # 493
hex_num = 0xFF            # 255
// C / C++
int binary = 0b1010;      // 10 (C++14+)
int octal = 0755;         // 493 (leading zero!)
int hex = 0xFF;           // 255

Warning: In C/C++, a leading zero means octal!

int x = 010;  // This is 8, not 10!

Bitwise Operations

Understanding binary makes bitwise operations intuitive:

AND (&) - Both bits must be 1

  1010 (10)
& 1100 (12)
──────
  1000 (8)

OR (|) - Either bit can be 1

  1010 (10)
| 1100 (12)
──────
  1110 (14)

XOR (^) - Bits must be different

  1010 (10)
^ 1100 (12)
──────
  0110 (6)

Left Shift (<<) - Multiply by 2

0001 << 1 = 0010  (1 becomes 2)
0001 << 3 = 1000  (1 becomes 8)

Right Shift (>>) - Divide by 2

1000 >> 1 = 0100  (8 becomes 4)
1000 >> 2 = 0010  (8 becomes 2)

Practical Uses

// Check if number is even
if ((n & 1) === 0) { /* even */ }

// Check if bit is set
if (flags & 0b0100) { /* bit 2 is set */ }

// Set a bit
flags = flags | 0b0100;

// Clear a bit
flags = flags & ~0b0100;

// Toggle a bit
flags = flags ^ 0b0100;

Quick Reference Table

DecimalBinaryHexOctal
00000 000000000
10000 000101001
70000 011107007
80000 100008010
100000 10100A012
150000 11110F017
160001 000010020
320010 000020040
640100 000040100
1000110 010064144
1270111 11117F177
1281000 000080200
2551111 1111FF377
2561 0000 0000100400

Key Takeaways

  1. Binary (base-2): The native language of computers -0s and 1s
  2. Hex (base-16): Compact binary representation, 1 hex digit = 4 bits
  3. Octal (base-8): Mainly used for Unix permissions, 1 octal digit = 3 bits
  4. Prefixes: 0b (binary), 0o (octal), 0x (hex)
  5. Colors: Hex codes are RGB values in hexadecimal
  6. Permissions: chmod uses octal (755, 644, etc.)

Use our Number Base Converter to practice conversions and verify your calculations.

Related Tools

Related Articles

Try Our Free Tools

200+ browser-based tools for developers and creators. No uploads, complete privacy.

Explore All Tools