👋 Welcome to the Binary Number System!
Welcome to the fundamental building block of all computer science! This chapter, "The Binary Number System," is part of your journey into Representing Data. Everything a computer does—from displaying a photo to running complex calculations—boils down to ones and zeros.
Don't worry if numbers aren't your favourite subject. We will break down these concepts, compare different number systems (Binary, Decimal, Hexadecimal), and learn the clever tricks computers use to handle tricky things like negative numbers and fractions. Let's get started!
1. The Foundation: Number Bases and Units (3.5.1 & 3.5.2)
1.1 Understanding Different Number Bases (Base 10, 2, 16)
We are used to counting in Decimal, or Base 10. Computers, however, operate using electricity (on or off), so they use Binary, or Base 2.
- Decimal (Base 10): Uses 10 digits (0–9). Each column represents a power of 10 (units, tens, hundreds, thousands...).
- Binary (Base 2): Uses only 2 digits (0 and 1). Each column represents a power of 2 (1, 2, 4, 8, 16...). These single digits are called Bits.
- Hexadecimal (Base 16): Uses 16 symbols (0–9 and A–F). This system is a shorthand for binary, making long binary strings easier for programmers to read.
Did you know? 'Hex' uses A=10, B=11, C=12, D=13, E=14, F=15.
1.2 Units of Information
The smallest unit of information is a Bit (a single 0 or 1). However, data is typically grouped for processing:
- A Byte is a group of 8 bits.
-
The number of unique values you can represent with \(n\) bits is \(2^n\).
Example: With 3 bits, you can represent \(2^3 = 8\) different values (from 000 to 111).
1.3 Binary vs. Decimal Prefixes (The Kilo-Controversy)
When talking about computer memory size, we use prefixes, but there are two different types:
Binary Prefixes (Powers of 2) - Used for storage/memory:
These are based on powers of \(2^{10}\) (1024). They are often used to define memory size precisely.
- Kibi (Ki): \(2^{10}\) (1,024)
(e.g. 1 KiB = 1024 Bytes) - Mebi (Mi): \(2^{20}\) (1,048,576)
- Gibi (Gi): \(2^{30}\)
- Tebi (Ti): \(2^{40}\)
Decimal Prefixes (Powers of 10) - Used for communication speeds:
These are the standard prefixes we use in mathematics and science, based on powers of \(10^3\) (1000).
- kilo (k): \(10^3\) (1,000)
(e.g. 1 kB = 1000 Bytes) - mega (M): \(10^6\) (1,000,000)
- giga (G): \(10^9\)
- tera (T): \(10^{12}\)
Key Takeaway: Computers communicate in binary (Base 2). Hexadecimal (Base 16) is human-friendly shorthand. Remember the difference between kibi (\(2^{10}\)) and kilo (\(10^3\)) when discussing data capacity.
1.4 Conversions Between Bases
Although conversions won't be examined independently, you need to be able to do them for other topics (like floating point numbers or assembly language).
Decimal to Binary Conversion (Example: Convert 42 Base 10 to Binary)
- Identify the largest power of 2 that fits into 42 (which is 32). Put a 1 in that column.
- Subtract 32 from 42. Remainder is 10.
- Check the next power of 2 (16). Does 16 fit into 10? No. Put a 0.
- Check 8. Does 8 fit into 10? Yes. Put a 1. Remainder is 2.
- Check 4. Does 4 fit into 2? No. Put a 0.
- Check 2. Yes. Put a 1. Remainder is 0.
- Check 1. No. Put a 0.
4210 = 001010102 (in 8 bits)
Binary to Hexadecimal Conversion (Quick Method)
- Group the binary digits into sets of 4, starting from the right. (Each group of 4 bits is called a nibble and corresponds exactly to one Hex digit).
- Convert each nibble into its Hex equivalent.
Example: 10110011
Group 1: 1011 (8 + 2 + 1) = 11. In Hex, this is B.
Group 2: 0011 (2 + 1) = 3. In Hex, this is 3.
Result: 101100112 = B316
2. Representing Positive Integers: Unsigned Binary (3.5.3.1)
2.1 Unsigned Binary vs. Signed Binary
Unsigned Binary is the simplest form, where all bits are used to represent the magnitude (size) of a positive number. There is no representation for negative values.
Signed Binary (covered next) reserves one bit (usually the Most Significant Bit or MSB) to indicate whether the number is positive or negative.
2.2 Range of Unsigned Integers
If you have \(n\) bits, the range of values you can represent in unsigned binary is:
Minimum Value: 0
Maximum Value: \(2^n - 1\)
Example: Using 8 bits (\(n=8\))
Maximum value: \(2^8 - 1 = 256 - 1 = 255\).
Range: 0 to 255.
2.3 Unsigned Binary Arithmetic (3.5.3.2)
Binary addition and multiplication follow rules similar to decimal arithmetic, but you only carry a 1 when the sum is 2 or more.
Binary Addition Rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 1 = 0 (carry 1)
- 1 + 1 + 1 (carry in) = 1 (carry 1)
Example Addition (12 + 5 = 17):
1 1 0 0 (12)
+ 0 1 0 1 (5)
-----
1 0 0 0 1 (17)
Common Mistake: When performing arithmetic, if the result requires more bits than allocated, you get an overflow error (covered in section 5).
Key Takeaway: Unsigned binary is easy, represents only positive numbers, and the maximum value is always \(2^n - 1\).
3. Representing Negative Integers: Two's Complement (3.5.3.3)
Two's complement is the only signed binary representation method you need to know. It allows computers to perform subtraction using standard addition circuitry, which simplifies hardware design.
3.1 Identifying the Sign
In Two's Complement:
- The Most Significant Bit (MSB) (the leftmost bit) indicates the sign.
- If MSB is 0, the number is positive.
- If MSB is 1, the number is negative.
3.2 Range of Two's Complement Integers
Since one bit is reserved for the sign, the maximum range is smaller than unsigned binary for the same number of bits.
For \(n\) bits, the range is:
Maximum Positive Value: \(2^{n-1} - 1\)
Minimum Negative Value: \(-2^{n-1}\)
Example: Using 8 bits (\(n=8\))
Maximum positive: \(2^7 - 1 = 127\).
Minimum negative: \(-2^7 = -128\).
Range: -128 to +127.
3.3 Converting Negative Decimal to Two's Complement Binary
Step-by-Step Trick (The "Flip and Add 1" Method):
Goal: Represent -5 in 8 bits.
- Find the positive binary: +5 in 8 bits is 0000 0101.
-
Invert (Flip) the bits: Change all 0s to 1s and all 1s to 0s (This is called the One's Complement).
0000 0101 becomes 1111 1010. -
Add 1 to the result:
1111 1010 + 1 = 1111 1011.
This is -5 in Two's Complement. (Note the MSB is 1, indicating a negative number).
3.4 Performing Subtraction using Two's Complement
The genius of Two's Complement is that subtraction (A - B) is just addition (A + (-B)).
Example: Calculate 12 - 5 (in 8 bits)
- Represent 12: 0000 1100
- Represent -5: 1111 1011 (from step 3.3)
-
Add them together:
0000 1100 (+12)
+ 1111 1011 (-5)
----------
(1) 0000 0111 (+7)
The final carry bit (1) is ignored if the calculation result fits within the allocated bits. The result 0000 0111 is 7, which is correct.
Key Takeaway: Two's Complement allows computers to represent negative numbers and perform subtraction using addition. The MSB tells you the sign.
4. Representing Fractional Numbers (3.5.3.4, 3.5.3.8, 3.5.3.9)
To represent numbers with decimal parts (real numbers), computers use two main methods: Fixed Point and Floating Point.
4.1 Fixed Point Representation
In Fixed Point, the position of the binary point is predetermined and stays "fixed."
- We allocate a fixed number of bits for the integer part (to the left of the point) and a fixed number of bits for the fractional part (to the right of the point).
- The powers used to the right of the point are negative powers of 2: \(2^{-1}\) (0.5), \(2^{-2}\) (0.25), \(2^{-3}\) (0.125), etc.
Example (4 bits for integer, 4 bits for fraction):
1 0 1 1 . 0 1 0 0
\( (8+0+2+1) . (0.25) \) = 11.25
4.2 Floating Point Representation (Simplified)
Floating point is the computer's version of scientific notation (e.g., \(6.022 \times 10^{23}\)). The binary point "floats" to maximise the number of significant digits represented.
A floating point number is represented by two main parts, both usually held using Two's Complement:
- The Mantissa (M): Represents the significant digits of the number (the precision).
- The Exponent (E): Represents the power of 2 by which the mantissa is multiplied (the range).
The value is calculated as: \( M \times 2^E \)
4.2.1 Range vs. Precision
The way bits are allocated between the Mantissa and Exponent determines the capability of the system:
- Precision: Determined by the number of bits in the Mantissa. More Mantissa bits = more accurate representation of the number (less rounding error).
- Range: Determined by the number of bits in the Exponent. More Exponent bits = ability to store much larger or much smaller numbers.
4.2.2 Normalisation (3.5.3.9)
To ensure every number has a unique representation and to maximise precision, floating point numbers must be Normalised.
Why Normalise? To put the binary point in a standard place, which eliminates redundant zeros.
Normalisation Rule: The mantissa must start with "01..." for a positive number or "10..." for a negative number.
How to Normalise (The Process):
- Shift the binary point until the mantissa starts with 01 (positive) or 10 (negative).
- Update the Exponent to reflect how many places you shifted the point.
Example: Un-normalised positive number 001100 \(\times 2^2\) (6 Mantissa bits, 4 Exponent bits)
1. Shift point 1 place to the right: 0.1100.
2. New Exponent: \(2^2\) becomes \(2^{2-1} = 2^1\).
Result: 011000 \(\times 2^1\). (The leading 0 is for the sign bit, the first number after the point is the '1' to satisfy 01...).
4.3 Comparing Fixed Point and Floating Point (3.5.3.8)
| Fixed Point | Floating Point | |
| Range | Very limited. | Very large (determined by Exponent). |
| Precision | Constant, but limited (determined by fraction bits). | High (determined by Mantissa). |
| Speed | Faster calculation (simpler arithmetic). | Slower calculation (complex hardware needed for shifting/normalisation). |
| Use | Simple financial systems (where precision must be fixed). | Scientific modelling, graphics, large calculations. |
Key Takeaway: Floating Point provides a huge range (Exponent) and good precision (Mantissa), but Fixed Point is simpler and faster for calculations where numbers don't vary greatly in size.
5. Limitations and Errors in Representation (3.5.3.5, 3.5.3.6, 3.5.3.7)
5.1 Rounding Errors (Inaccuracy) (3.5.3.5)
Binary systems cannot accurately represent all decimal fractions, similar to how we cannot write 1/3 perfectly in decimal (it is 0.3333...).
- A decimal number (like 0.1) may require an infinite number of binary digits to be truly exact.
- Since a computer uses a fixed number of bits (limited precision), it must truncate or round the representation, leading to small Rounding Errors.
5.2 Absolute and Relative Errors (3.5.3.6)
When a calculation is inaccurate, we need a way to measure the error:
-
Absolute Error: The actual difference between the true value and the represented value.
Formula: \( | \text{True Value} - \text{Stored Value} | \) -
Relative Error: The absolute error divided by the true value (expressed as a percentage or fraction). This is often more useful because it shows the error relative to the size of the number.
Formula: \( \frac{\text{Absolute Error}}{\text{True Value}} \)
Example: An absolute error of 100 is huge if the true value is 200 (50% relative error), but tiny if the true value is 1,000,000 (0.01% relative error).
5.3 Overflow and Underflow (3.5.3.7)
These errors occur when the result of a calculation is outside the range that the allocated bits can represent.
-
Overflow: Occurs when the result is too large (too positive or too negative) to be stored in the available number of bits.
Imagine trying to store 300 in an 8-bit unsigned system (max 255). It overflows.
-
Underflow: Occurs when the result is too close to zero to be represented accurately, meaning the closest possible representation the system can store is 0.
This happens mostly with very small fractional numbers (floating point) where the exponent is too small to cope.
Key Takeaway: Computers use finite bits, leading to inherent limitations (rounding errors). Overflow and underflow errors define the boundaries of the system's range.
🧠 Quick Review Checklist
- Can I convert between Binary, Decimal, and Hexadecimal?
- Can I calculate the unsigned range of an n-bit number? (0 to \(2^n - 1\))
- Can I calculate the signed range of an n-bit number? (\(-2^{n-1}\) to \(2^{n-1} - 1\))
- Can I use Two's Complement to represent a negative number and perform subtraction?
- Do I understand that the Mantissa controls precision and the Exponent controls range in floating point?
- Can I explain Normalisation and why it's necessary?
- Do I know the difference between Overflow (too big) and Underflow (too close to zero)?
Congratulations! You have covered the core concepts of how numbers are managed inside a computer. Keep practicing those conversions and two's complement steps!