Introduction: Why Number Bases Matter
Welcome to the world of Computer Science data representation! This chapter might seem like pure mathematics at first, but it is the absolute foundation of how computers work. Why? Because computers don't speak English or Decimal (Base 10); they speak in Binary (Base 2) – sequences of ones and zeros.
Understanding number bases and conversions will allow you to see exactly how data is stored, manipulated, and represented inside the processor. This knowledge is essential for later topics like assembly language, floating-point numbers, and data storage calculations.
1. The Core Number Bases (3.5.1)
1.1 Decimal (Base 10) – Our Everyday System
We use the Decimal system every day. It is Base 10 because it uses 10 unique digits (0 to 9).
- Base: 10
- Digits Used: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
We understand value through Positional Notation, where the position of a digit determines its power of 10.
Example: The number 459 in Base 10 is actually:
$$4 \times 10^2 + 5 \times 10^1 + 9 \times 10^0$$
1.2 Binary (Base 2) – The Computer's Language
Computers use Binary because electronic circuits can only represent two states: ON (1) or OFF (0). Each 1 or 0 is called a Bit (Binary Digit).
- Base: 2
- Digits Used: 0, 1
Binary also uses positional notation, but based on powers of 2.
Example: An 8-bit binary number uses these column weights:
$$128, 64, 32, 16, 8, 4, 2, 1$$
1.3 Hexadecimal (Base 16) – The Programmer's Shorthand
Binary strings (like 110101100101) quickly become very long and difficult for humans to read and remember. Hexadecimal (Hex) is a convenient shorthand for binary.
- Base: 16
- Digits Used: 0-9 and A-F
Hex uses 16 unique symbols. Since we only have 10 numeric digits, the letters A through F are used to represent the values 10 through 15.
| Decimal | Binary (4 bits) | Hexadecimal |
|---|---|---|
| 0 | 0000 | 0 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 15 | 1111 | F |
2. Number Base Conversions (3.5.1)
You must be able to convert numbers between all three bases: Decimal (Base 10), Binary (Base 2), and Hexadecimal (Base 16).
2.1 Converting Binary to Decimal (Base 2 to Base 10)
This is the easiest conversion: multiply each digit by its corresponding power of 2 and sum the results.
Step-by-Step Example: Convert \(101101_2\) to Decimal.
- Write out the positional column headings (powers of 2, starting from the right with $2^0 = 1$).
- Place the binary digits under the headings.
- Add up the values where a '1' appears.
| $2^5 (32)$ | $2^4 (16)$ | $2^3 (8)$ | $2^2 (4)$ | $2^1 (2)$ | $2^0 (1)$ |
|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 | 1 |
$$32 + 0 + 8 + 4 + 0 + 1 = 45$$
Result: \(101101_2 = 45_{10}\)
2.2 Converting Decimal to Binary (Base 10 to Base 2)
The most reliable method for larger numbers is the Repeated Division by 2 method, collecting the remainders in reverse order.
Step-by-Step Example: Convert \(45_{10}\) to Binary.
- Divide the decimal number by 2.
- Note the remainder (0 or 1).
- Repeat the process with the quotient until the quotient is 0.
- Read the remainders from bottom (MSB) to top (LSB).
| Division | Quotient | Remainder (Bit) |
|---|---|---|
| $45 \div 2$ | 22 | 1 |
| $22 \div 2$ | 11 | 0 |
| $11 \div 2$ | 5 | 1 |
| $5 \div 2$ | 2 | 1 |
| $2 \div 2$ | 1 | 0 |
| $1 \div 2$ | 0 | 1 |
Reading up: 101101.
Result: \(45_{10} = 101101_2\)
Key Takeaway for Conversions: Binary and Hexadecimal conversions are crucial for representing the internal structure of data (like memory addresses or instruction codes).
2.3 Converting Binary and Hexadecimal
This conversion is direct and easy because $16 = 2^4$. One hexadecimal digit perfectly represents exactly four binary bits.
Binary to Hex: Grouping by Fours
Start from the right (LSB) and group the binary digits into sets of four. Convert each group into its single Hex equivalent.
Example: Convert \(1101011011_2\) to Hex.
- Pad the left side with zeros to make full groups of 4: 0011 0101 1011
- Convert each group:
- $0011_2 = 3_{16}$
- $0101_2 = 5_{16}$
- $1011_2 = B_{16}$ (Since $11_{10} = B_{16}$)
- Combine: 35B
Result: \(1101011011_2 = 35B_{16}\)
Hex to Binary: Expanding Each Digit
Convert each Hex digit into its equivalent 4-bit binary sequence.
Example: Convert \(E7_{16}\) to Binary.
- Convert E: $E_{16} = 14_{10} = 1110_2$
- Convert 7: $7_{16} = 7_{10} = 0111_2$
- Combine: 11100111
Result: \(E7_{16} = 11100111_2\)
Quick Review: Conversion Tips
Binary $\leftrightarrow$ Hex: Always use the 4-bit grouping shortcut. It's fast and error-free.
Decimal to Binary: Use Repeated Division by 2, reading remainders upwards.
Binary to Decimal: Write out the powers of 2 (128, 64, 32, 16, 8, 4, 2, 1) and sum the active positions.
3. Units of Information (3.5.2)
3.1 Bits and Bytes
The smallest unit of information is the bit (b). A bit stores a 0 or 1.
The fundamental grouping used in computer memory and storage is the byte (B).
- A Byte is a group of 8 bits.
- A byte can represent $2^8 = 256$ different values (from 0 to 255).
3.2 The \(2^n\) Rule
If you have $n$ bits, the total number of different values you can represent is \(2^n\).
Did you know? The amount of memory you can address in a computer system is also determined by the number of bits in the address bus (e.g., a 32-bit address bus can access $2^{32}$ locations).
Example:
- 2 bits can represent $2^2 = 4$ values (00, 01, 10, 11).
- 3 bits can represent $2^3 = 8$ values (000 to 111).
- 16 bits can represent $2^{16} = 65,536$ values.
3.3 Decimal vs. Binary Prefixes
When discussing storage quantities (kilobytes, megabytes, etc.), there are two systems based on powers of 10 or powers of 2. It is vital to know the difference between the standard decimal prefixes (kilo, mega) and the binary prefixes (kibi, mebi).
Decimal Prefixes (Powers of 10)
These are the standard units used in mathematics and by most storage manufacturers (e.g., hard drives).
| Name | Symbol | Power of 10 | Approximate Value |
|---|---|---|---|
| kilo | k | $10^3$ | 1,000 |
| mega | M | $10^6$ | 1,000,000 |
| giga | G | $10^9$ | 1,000,000,000 |
| tera | T | $10^{12}$ | $10^{12}$ |
Example: 1 kB (kilobyte) = $10^3$ bytes (1,000 bytes)
Binary Prefixes (Powers of 2)
These prefixes are used by computer systems to refer to memory and data sizes, as everything is based on powers of 2. The standard binary prefixes use "bi" in the middle (kibi, mebi, gibi, tebi).
| Name | Symbol | Power of 2 | Exact Value |
|---|---|---|---|
| kibi | Ki | $2^{10}$ | 1,024 |
| mebi | Mi | $2^{20}$ | 1,048,576 |
| gibi | Gi | $2^{30}$ | $2^{30}$ |
| tebi | Ti | $2^{40}$ | $2^{40}$ |
Example: 1 KiB (kibibyte) = $2^{10}$ bytes (1,024 bytes)
Analogy: Think of this as measuring length. The decimal kilo is exactly 1000 meters. The binary kibi is slightly longer, at 1024 meters.
4. Representing Integers in Binary (3.5.3)
4.1 Unsigned Binary (3.5.3.1)
Unsigned binary is used to represent positive whole numbers (and zero). The term "unsigned" means there is no bit dedicated to indicating whether the number is positive or negative.
- All bits contribute to the magnitude (size) of the number.
- The minimum value is always 0.
- The maximum value for $n$ bits is \(2^n - 1\).
Example: In 8-bit unsigned binary, the range is 0 to $2^8 - 1$ (0 to 255).
4.2 Unsigned Binary Arithmetic (3.5.3.2)
Binary Addition
Binary addition follows simple rules, just like decimal addition, but the carry occurs when the sum reaches 2.
| $A$ | $B$ | Sum | Carry |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 (Carry the 1) |
| 1 + 1 + Carry 1 | 1 | 1 (Carry the 1) |
Example: \(5_{10} + 3_{10}\) in 4 bits
1 1 0 0 <-- Carries
0 1 0 1 (5)
+ 0 0 1 1 (3)
-----------------
1 0 0 0 (8)
Dealing with Overflow
If the result of an addition exceeds the maximum range of the allocated bits, an overflow occurs. This extra bit is lost, leading to an incorrect result.
Example: Adding $15 + 1$ in 4-bit unsigned binary:
1 1 1 1 (15)
+ 0 0 0 1 (1)
-----------------
(1) 0 0 0 0 (0 - Incorrect!)
The 5th bit (1) is the overflow and is discarded, meaning $15 + 1$ appears to equal 0.
Binary Multiplication
Binary multiplication uses simple shifts and additions, similar to long multiplication in decimal, but we only ever multiply by 0 or 1.
Example: \(101_2 \times 11_2\) (\(5 \times 3\))
1 0 1
x 0 1 1
-------
1 0 1 (101 x 1)
+ 1 0 1 0 (101 x 1 shifted left)
-------
1 1 1 1 (15)
Result: \(1111_2 = 15_{10}\)
Always remember the carry bit in binary addition! If you are working in a fixed number of bits (like 8 bits), any carry past the last position is an overflow.
4.3 Signed Binary using Two's Complement (3.5.3.3)
To represent negative numbers, we use signed binary. The only method you need to know for this syllabus is Two's Complement.
In Two's Complement, the Most Significant Bit (MSB)—the leftmost bit—acts as the sign indicator:
- If MSB is 0, the number is positive.
- If MSB is 1, the number is negative.
Converting Positive Decimal to Two's Complement
This is the same as converting to unsigned binary, but you must ensure there is a leading zero if you are working within a specific bit length (e.g., 8 bits).
Example: +13 in 8 bits is \(00001101_2\).
Converting Negative Decimal to Two's Complement
Follow the "Flip and Add 1" method:
- Find the positive representation of the number in binary (e.g., for -13, use +13).
- Flip all the bits (0s become 1s, and 1s become 0s). This is the One's Complement.
- Add 1 to the result of the flip.
Step-by-Step Example: Convert \(-13_{10}\) to 8-bit Two's Complement.
- Positive 13: \(00001101\)
- Flip the bits (One's Complement): \(11110010\)
- Add 1:
11110010 + 1 -------- 11110011
Result: \(-13_{10} = 11110011_2\). (Note the MSB is 1, confirming it is negative).
Converting Two's Complement Back to Decimal
If the MSB is 0, convert normally (it's positive).
If the MSB is 1 (it's negative), you can use the shortcut: treat the MSB as a negative weight, and sum the rest normally.
Example: Convert \(11110011_2\) back to Decimal (8 bits).
| $-2^7 (-128)$ | $2^6 (64)$ | $2^5 (32)$ | $2^4 (16)$ | $2^3 (8)$ | $2^2 (4)$ | $2^1 (2)$ | $2^0 (1)$ |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
$$(-128) + 64 + 32 + 16 + 0 + 0 + 2 + 1 = -13$$
Two's Complement Subtraction
In computer arithmetic, subtraction (A - B) is performed by adding the negative of B: $$A - B = A + (-B)$$
Step-by-Step Example: \(25_{10} - 10_{10}\) in 8 bits.
We perform $25 + (-10)$.
- Positive 25: $00011001$
- Negative 10 (\(-10_{10}\)):
- Positive 10: 00001010
- Flip: 11110101
- Add 1: 11110110
- Add the two binary numbers:
1 1 1 1 1 0 0 0 <-- Carries 0 0 0 1 1 0 0 1 (25) + 1 1 1 1 0 1 1 0 (-10) ------------------ (1) 0 0 0 0 1 1 1 1 (15)
The final carry (in brackets) is discarded in Two's Complement arithmetic. The remaining 8 bits, $00001111_2$, converts to $15_{10}$.
Result: The subtraction is successfully completed using addition.
The Range of n bits in Two's Complement
Since one bit is dedicated to the sign, the maximum range is smaller than unsigned binary.
For $n$ bits, the range is:
$$\text{Minimum Value: } -2^{n-1}$$ $$\text{Maximum Value: } +2^{n-1} - 1$$
Example: For 8 bits ($n=8$):
- Minimum: $-2^{(8-1)} = -2^7 = -128$
- Maximum: $2^{(8-1)} - 1 = 2^7 - 1 = 127$
Key Takeaway: Binary Representation
The choice of Unsigned vs. Signed (Two's Complement) determines the range of numbers you can store. Unsigned gives you the largest positive range (e.g., 0 to 255 in 8 bits), while Two's Complement allows you to store both positive and negative integers (e.g., -128 to 127 in 8 bits).