Binary & Number Systems
Understand how computers represent all data using binary, and how different number systems like decimal, octal, and hexadecimal relate to each other.
The Language of Silicon: Why Binary?
To the human eye, a computer displays high-resolution photos, streamable video, complex games, and responsive text. Yet, deep inside the computer's CPU and memory chips, there is nothing but an endless sea of microscopic switches called transistors. These transistors have only two possible states: ON (representing presence of electrical voltage) or OFF (representing absence of electrical voltage). Because all digital hardware is fundamentally built upon this two-state architecture, the binary (base-2) number system is the natural language of computing.
If computers used the decimal (base-10) system at the hardware level, engineers would need to design transistors capable of reliably distinguishing between ten different voltage levels (e.g., 0V, 0.5V, 1.0V, up to 4.5V). In practice, electrical interference, heat, and component degradation would cause constant errors, as a 3.9V signal could easily degrade into 3.8V, misrepresenting a number. By utilizing only two statesโfully on or fully offโcomputers achieve near-perfect reliability, speed, and noise resistance.
Understanding Positional Number Systems
All the number systems used in computer science are positional notation systems. In a positional system, the value of a digit depends on its position relative to the radix point (decimal point). Each position represents a power of the base (or radix) of that system.
For example, in our daily Decimal (Base-10) system, the number 345 represents:
(3 * 10^2) + (4 * 10^1) + (5 * 10^0) = 300 + 40 + 5 = 345 In computer science, four major positional number systems are widely used:
| System | Base | Allowed Digits / Symbols | Common Use Case in CS |
|---|---|---|---|
| Binary | 2 | 0, 1 | Machine code, logic circuits, network subnetting, bitwise operations. |
| Octal | 8 | 0, 1, 2, 3, 4, 5, 6, 7 | Unix file permissions (e.g., chmod 755), legacy mainframes. |
| Decimal | 10 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | Human-readable input/output, user interfaces, accounting. |
| Hexadecimal | 16 | 0-9, A, B, C, D, E, F | Memory addressing (RAM), CSS color codes, debugging core dumps. |
Deep Dive: Hexadecimal (Base-16)
While binary is perfect for hardware, a 32-bit memory address looks like this in binary: 11000000101010000000000100000001. This is extremely difficult for human developers to read, write, or debug without making mistakes. To bridge the gap, computer scientists use hexadecimal (Base-16).
Because 16 is a power of 2 (2^4 = 16), exactly one hexadecimal digit can represent a sequence of four binary digits (a **nibble**). Hexadecimal uses digits 0 through 9, followed by letters A through F to represent values 10 through 15:
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
Using hex, that massive 32-bit address simplifies to a clean, readable string: 0xC0A80101 (where the 0xC0 corresponds to 11000000, 0xA8 to 10101000, and so on). The 0x prefix is commonly used in programming to signal that the following digits are hexadecimal.
Step-by-Step Number System Conversions
1. Converting Binary to Decimal
To convert a binary number to decimal, multiply each bit by 2 raised to the power of its position index (starting at 0 from the rightmost bit) and sum the results. Let's convert 1101 to decimal:
Binary Digit: 1 1 0 1
Position: 3 2 1 0
Multiplier: 2^3 2^2 2^1 2^0
Value: 8 4 0 1
Calculation: (1 * 8) + (1 * 4) + (0 * 2) + (1 * 1) = 8 + 4 + 0 + 1 = 13 2. Converting Decimal to Binary (Successive Division)
To convert a decimal number to binary, divide the decimal number by 2 repeatedly, recording the remainder (which will always be 0 or 1). Read the remainders from the bottom up. Let's convert decimal 19 to binary:
19 / 2 = 9 remainder 1 (Least Significant Bit - rightmost)
9 / 2 = 4 remainder 1
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1 (Most Significant Bit - leftmost)
Result (bottom to top): 10011 3. Converting Binary to Hexadecimal
Group the binary number into sets of four bits starting from the right. If the leftmost group has fewer than four bits, pad it with leading zeroes. Then, replace each group of four bits with its equivalent hexadecimal character. Let's convert 110101111 to hex:
Step 1: Group into fours: [0001] [1010] [1111] (padded leftmost with three 0s)
Step 2: Convert each: [1] [10] [15]
Step 3: Map to Hex digits: 1 A F
Result: 1AF (or 0x1AF) Binary Arithmetic: The Mechanics of the ALU
Digital processors perform addition and subtraction using circuits designed around logic gates. The rules of binary arithmetic are incredibly straightforward because there are no tables to memorize beyond 0 and 1.
Binary Addition Rules
0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 0(carry 1 to the next column)1 + 1 + 1 = 1(carry 1 to the next column)
Example: Adding 1011 (11) and 0101 (5):
Carry: 1 1 1
1 0 1 1 (11)
+ 0 1 0 1 (5)
---------
1 0 0 0 0 (16) Binary Subtraction and Two's Complement
CPUs do not have separate physical circuits for subtraction. Instead, they convert subtraction into addition using a mathematical representation called Two's Complement. To make a binary number negative:
- Perform the **bitwise NOT** operation (flip all 1s to 0s, and 0s to 1s). This is the One's Complement.
- Add `1` to the resulting value. This yields the Two's Complement.
To subtract A - B, the CPU calculates A + (-B) using the Two's Complement representation of B. This elegant system eliminates hardware complexity, allowing the exact same adder circuits to handle both positive and negative math.
Digital Encoding of Text: ASCII and Unicode
If computers only understand binary, how do they represent letters, punctuation, and emoji? The answer is **character encoding schemas**.
- ASCII (American Standard Code for Information Interchange): A legacy 7-bit encoding standard developed in the 1963. It maps numbers 0โ127 to English letters, numbers, and system commands (e.g., capital letter 'A' is mapped to decimal 65, which is binary
01000001). ASCII is highly efficient but cannot represent non-English alphabets or symbols. - Unicode: A universal standard that assigns a unique number (a code point) to every character, symbol, and emoji across all writing systems globally.
- UTF-8: A variable-width encoding for Unicode that is backwards-compatible with ASCII. It uses between 1 and 4 bytes (8 to 32 bits) per character. Because it is highly space-efficient for English text while supporting any language, UTF-8 is the dominant encoding standard for the World Wide Web.
Frequently Asked Questions
What is the difference between a bit and a byte?
A bit (binary digit) is the absolute smallest unit of digital data, representing a simple choice of 0 or 1. A byte is a collection of exactly 8 bits. Bytes are the standard building blocks of computer memory, as they are large enough to store a single ASCII character or integer values between 0 and 255.
Why do hexadecimal numbers often start with 0x?
The 0x prefix is a syntax convention used in programming languages (like C, C++, Java, and JavaScript) to tell the compiler or interpreter that the following text represents a hexadecimal value rather than a decimal integer or variable name.
What is a byte overflow?
A byte overflow occurs when a mathematical operation produces a number that is too large to fit within the allocated memory width. For example, an unsigned 8-bit byte can store values from 0 to 255. If you add 1 to 255 (binary 11111111), the result rolls over back to 0 (binary 00000000) because there is no 9th bit available to hold the carry value, leading to logic bugs.
How many different values can be represented by n bits?
The number of unique values that can be represented by n bits is calculated as 2 raised to the power of n (2^n). For example, 8 bits can represent 2^8 = 256 unique values, while 16 bits can represent 2^16 = 65,536 values.
What's Next?
Now that you know how computers represent data numerically and textually using binary, you can explore where this binary data is physically stored while processing. Read our next guide on Memory & Storage to understand the memory hierarchy, registers, cache, and permanent storage media.