What is binary?
Why computers speak in 0s and 1s
You count using ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. This is the decimal system (base-10), and you use it because you have ten fingers.
Computers do not have fingers. They have transistors, tiny electronic switches. A computer's circuits treat each one as either on or off. There is no "half-on" or "sort of off" as far as the logic is concerned. Just two states.
Because of this physical limitation, computers use the binary system (base-2), which has only two digits: 0 and 1.
Transistor OFF = 0
Transistor ON = 1
Every number, every letter, every pixel of every image, every byte of every Ruby file: all of it is ultimately stored as a sequence of 0s and 1s inside your computer.
Counting in binary
In decimal, when you run out of digits (after 9), you add a new column to the left and reset:
8, 9, 10, 11, 12, ...
Binary works the same way, but you run out of digits after 1:
Decimal: 0 1 2 3 4 5 6 7 8
Binary: 0 1 10 11 100 101 110 111 1000
Let us break down how binary columns work:
Decimal columns (base-10):
1000s 100s 10s 1s
1 2 5 3 = 1×1000 + 2×100 + 5×10 + 3×1 = 1253
Binary columns (base-2):
128s 64s 32s 16s 8s 4s 2s 1s
1 0 0 1 1 0 1 0 = 128+16+8+2 = 154
Each column in binary is a power of 2 (1, 2, 4, 8, 16, 32, 64, 128...), just like each column in decimal is a power of 10.
The powers of 2: a developer's best friend
These numbers will appear everywhere in your career:
| Power | Value | Where You See It |
|---|---|---|
| 2⁰ | 1 | A single bit |
| 2¹ | 2 | Boolean: true/false |
| 2³ | 8 | One byte (8 bits) |
| 2⁸ | 256 | RGB color values (0-255) |
| 2¹⁰ | 1,024 | 1 KiB (often written KB) |
| 2¹⁶ | 65,536 | TCP port range |
| 2²⁰ | 1,048,576 | 1 MB (megabyte) |
| 2³⁰ | 1,073,741,824 | 1 GB (gigabyte) |
| 2³² | 4,294,967,296 | IPv4 address limit |
When a database column is integer (32-bit signed), its maximum value is 2³¹ - 1 = 2,147,483,647. Past that, you need bigint (64-bit). Rails has created bigint primary keys by default since Rails 5.1, so this limit mostly bites older tables and plain integer columns.
Binary in Ruby
Ruby lets you write numbers directly in binary using the 0b prefix:
0b1010 # => 10
0b11111111 # => 255
0b10000000 # => 128
You can also convert between decimal and binary:
# Decimal to binary string
42.to_s(2) # => "101010"
255.to_s(2) # => "11111111"
# Binary string to decimal
"101010".to_i(2) # => 42
"11111111".to_i(2) # => 255
Binary shows up in daily work. You will use binary operations when working with:
- File permissions (
chmod 755is binary flags) - Bitwise flags (feature toggles stored as a single integer)
- Network masks (IP subnets use binary AND operations)
- Color values (RGB channels packed into integers)
Converting decimal to binary by hand
To convert a decimal number to binary, repeatedly divide by 2 and collect the remainders:
42 ÷ 2 = 21 remainder 0 ─┐
21 ÷ 2 = 10 remainder 1 │
10 ÷ 2 = 5 remainder 0 │ Read remainders
5 ÷ 2 = 2 remainder 1 │ BOTTOM to TOP:
2 ÷ 2 = 1 remainder 0 │ 101010
1 ÷ 2 = 0 remainder 1 ─┘
42 in decimal = 101010 in binary ✓
Verification: 1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 32 + 8 + 2 = 42 ✓
Why this matters for Rails developers
You might wonder: "I write Ruby, not binary. Why should I care?"
-
Database IDs: When an
integerid column reaches 2,147,483,647 (2³¹ - 1), inserts fail. This app'susers.idis abigint, so it won't. Understanding binary tells you why and when. -
Character encoding: A UTF-8 emoji like 😀 takes 4 bytes (32 bits). PostgreSQL's
varchar(1)counts characters, so the emoji fits, but it still occupies 4 bytes. Any limit measured in bytes sees "1 character" ≠ "1 byte". -
Permissions:
chmod 755means111 101 101in binary: owner can read/write/execute, group and others can read/execute. -
Performance: Bitwise operations (
&,|,^,<<,>>) are among the fastest operations a CPU can perform, usually a single clock cycle at the hardware level.
Summary
| Concept | What It Means |
|---|---|
| Binary (base-2) | Number system with only 0 and 1 |
| Transistor | Physical switch: ON (1) or OFF (0) |
| Powers of 2 | Column values: 1, 2, 4, 8, 16, 32, 64, 128... |
0b prefix |
Ruby syntax for binary literals (0b1010 = 10) |
.to_s(2) |
Convert integer to binary string in Ruby |
.to_i(2) |
Convert binary string to integer in Ruby |
| 32-bit integer limit | 2,147,483,647: know when to use bigint |
Keep going with lesson 2
Bits and bytes: units of data
Bits, bytes, KB versus KiB, bits versus bytes in networking, and how Ruby measures string and object size.
A free account also gives you the quizzes, your progress and notes, and the discussion.