Binary Number System - How Binary Works in Computing

May 28, 20265 min read

What Is the Binary Number System?

The binary number system is a base-2 numeral system that uses exactly two digits: 0 and 1. Each digit — called a bit — represents a power of 2, just as each decimal digit represents a power of 10.

1011 = 1 × 2³ + 0 × 2² + 1 × 2¹ + 1 × 2⁰
     = 8 + 0 + 2 + 1
     = 11 (decimal)

Binary isn't just a convenient encoding choice. It mirrors the physical behavior of transistors — the microscopic switches inside every processor. A transistor conducts or blocks current: two states, naturally represented as 1 and 0.

Binary vs Decimal

Both systems work the same way — they just use different bases:

PropertyDecimal (Base-10)Binary (Base-2)
Digits0, 1, 2, ..., 90, 1
Place values10⁰, 10¹, 10², …2⁰, 2¹, 2², …
Example number42101010
Largest 3-digit999111 (= 7)

Decimal feels natural because humans have ten fingers. Binary feels natural to hardware because digital circuits have two voltage states. Every other number system — octal, hexadecimal — is just a more compact way for humans to read binary.

How to Convert Decimal to Binary

The standard method: divide by 2, record the remainder, and read remainders bottom-up.

Example: Convert 156 to Binary

156 ÷ 2 = 78  remainder 0
 78 ÷ 2 = 39  remainder 0
 39 ÷ 2 = 19  remainder 1
 19 ÷ 2 = 9   remainder 1
  9 ÷ 2 = 4   remainder 1
  4 ÷ 2 = 2   remainder 0
  2 ÷ 2 = 1   remainder 0
  1 ÷ 2 = 0   remainder 1

Read bottom-up: 10011100

Verify: 128 + 16 + 8 + 4 = 156

Shortcut: Power-of-2 Subtraction

Find the largest power of 2 that fits, subtract, and repeat:

156 - 128 = 28  → bit 7 = 1
 28 -  16 = 12  → bit 4 = 1
 12 -   8 = 4   → bit 3 = 1
  4 -   4 = 0   → bit 2 = 1
All other bits = 0

Result: 10011100 (same as above)

How to Convert Binary to Decimal

Multiply each bit by its place value and sum:

Binary: 1 0 0 1 1 1 0 0
Place:  7 6 5 4 3 2 1 0

1×128 + 0×64 + 0×32 + 1×16 + 1×8 + 1×4 + 0×2 + 0×1
= 128 + 16 + 8 + 4
= 156

Binary in Computer Science

Memory Addressing

Memory is addressed in bytes (8 bits). A 32-bit processor can address 2³² = 4,294,967,296 bytes (4 GB). A 64-bit processor can address 2⁶⁴ bytes — far more than any current system needs.

IP Addresses

IPv4 addresses are 32-bit binary numbers, written as four decimal octets for readability:

Binary:  11000000 10101000 00000001 00000001
Decimal: 192.168.1.1

Each octet ranges from 00000000 (0) to 11111111 (255). That's why no IPv4 octet exceeds 255.

Subnet masks are pure binary logic — an AND operation between the IP address and the mask yields the network address:

IP:    11000000.10101000.00000001.00000001  (192.168.1.1)
Mask:  11111111.11111111.11111111.00000000  (255.255.255.0)
AND:   11000000.10101000.00000001.00000000  (192.168.1.0)

Color Values

RGB colors use three bytes — one for red, one for green, one for blue. Pure red is 11111111 00000000 00000000, or #FF0000 in hex. Each channel's 8 bits give 256 intensity levels (0–255), yielding 16,777,216 possible colors.

File Permissions (Unix)

File permissions use 3 bits per role (read, write, execute):

rwx r-x r-- = 111 101 100 = 7 5 4

That's where chmod 754 comes from — each octal digit represents 3 binary permission bits.

Binary Representation in Code

JavaScript

// Binary literal (ES6+)
const mask = 0b11110000;  // 240

// Convert to binary string
(42).toString(2);  // "101010"

// Parse binary string
parseInt('101010', 2);  // 42

Python

# Binary literal
mask = 0b11110000  # 240

# Convert to binary string
bin(42)  # '0b101010'

# Parse binary string
int('101010', 2)  # 42

Common Binary Patterns

Binary PatternDecimalMeaning
000000000Minimum byte value
11111111255Maximum byte value
10000000128MSB set (sign bit in signed ints)
0000111115Low nibble mask
11110000240High nibble mask
0101010185Alternating bits
10101010170Alternating bits

Signed Numbers: Two's Complement

Computers represent negative integers using two's complement. To negate a number: flip all bits, then add 1.

+5 = 00000101
Flip: 11111010
Add 1: 11111011 = -5

Two's complement makes subtraction identical to addition — the CPU doesn't need separate subtract circuitry. The leftmost bit (MSB) acts as the sign bit: 0 for positive, 1 for negative.

In an 8-bit signed integer:

  • Range: -128 to +127
  • 10000000 = -128 (the most negative value)
  • 01111111 = +127 (the most positive value)

Key Takeaways

  • Binary is base-2: only digits 0 and 1, with place values as powers of 2
  • Convert decimal to binary by dividing by 2 and collecting remainders
  • IP addresses, RGB colors, and file permissions all rely on binary representations
  • Two's complement elegantly handles signed integers without special subtraction logic
  • Hexadecimal is simply a compact way to write binary — each hex digit maps to exactly 4 bits

Try It Yourself

Practice binary, octal, decimal, and hex conversions with our free Number Base Converter. Encode text to binary with the Text to Binary converter.