Welcome to the World of Binary!
Ever wondered how a computer, which is basically a collection of wires and plastic, can show you high-definition movies, play complex games, or help you write an essay? It all starts with something very simple: Binary. In this chapter, we’ll explore how computers use just two numbers—0 and 1—to do everything! Don't worry if it seems like a different language at first; by the end of these notes, you'll be "speaking computer" like a pro.
1. What is Binary and Why Do We Use It?
Computers are made of billions of tiny switches called transistors. These switches can only be in two states: ON (represented by 1) or OFF (represented by 0). This "two-state" system is what we call Binary.
Key Concepts
- Bit: The smallest unit of data. It stands for Binary Digit. It is either a 0 or a 1.
- Binary Pattern: A sequence of bits (like 1010) that represents data or instructions.
- States: The more bits you use, the more unique "patterns" or "states" you can create.
The Math Trick: How many states?
To find out how many different things a binary pattern can represent, use this simple formula: \(2^n\), where \(n\) is the number of bits.
Example: If you have 3 bits, you can have \(2^3\) (\(2 \times 2 \times 2\)) = 8 different states.
Did you know? Everything on a computer—images, music, and even the text you are reading right now—is stored as a long string of 0s and 1s!
Key Takeaway: Computers use binary because their hardware is made of switches that only have two states (on/off). We use the formula \(2^n\) to calculate how many values a set of bits can store.
2. Working with Numbers: Unsigned Integers
In the denary system (the base-10 system we use in daily life), we have units, tens, hundreds, etc. In binary (base-2), the place values double every time you move to the left.
8-bit Binary Table
For your exam, you need to know 8-bit numbers. Here are the place values:
122, 64, 32, 16, 8, 4, 2, 1
How to convert Binary to Denary
1. Draw the table.
2. Put your binary number (e.g., 10001010) into the table.
3. Add up the values where there is a 1.
Example: 10001010
\(128 + 8 + 2 = 138\)
How to convert Denary to Binary
1. Start from the left (128).
2. If your number is bigger than the place value, put a 1 and subtract the value.
3. If not, put a 0 and move to the next column.
Quick Review: Unsigned integers are always positive. An 8-bit unsigned integer can represent numbers from 0 to 255.
3. Signed Integers: Two's Complement
Computers also need to store negative numbers. We use a system called Two's Complement. In this system, the Most Significant Bit (the one on the far left) becomes negative.
The 8-bit Signed Table
-128, 64, 32, 16, 8, 4, 2, 1
The "Flip and Add 1" Trick
If you need to turn a positive number into a negative one:
1. Write the positive number in binary (e.g., 5 is 00000101).
2. Invert all the bits (0 becomes 1, 1 becomes 0) -> 11111010.
3. Add 1 to the result -> 11111011. This is -5!
Common Mistake: Don't forget that in Two's Complement, the range for 8 bits is -128 to +127. It is not the same as unsigned!
Key Takeaway: Two's Complement allows computers to represent negative numbers by making the furthest left bit a negative value.
4. Binary Addition
Adding binary is just like adding normal numbers, but you "carry over" much sooner!
The Four Rules:
- \(0 + 0 = 0\)
- \(0 + 1 = 1\)
- \(1 + 1 = 0\) (Carry 1)
- \(1 + 1 + 1 = 1\) (Carry 1)
What is Overflow?
Imagine a car's mile-counter only has 3 digits. If you reach 999 and go one more mile, it resets to 000. This is Overflow. In binary, if you add two 8-bit numbers and the result needs a 9th bit, that extra bit is lost. The computer might give a completely wrong answer or crash!
Key Takeaway: Binary addition follows simple rules, but overflow occurs when the result is too large to fit in the allocated number of bits.
5. Binary Shifts
Shifting bits left or right is a very fast way for a computer to multiply or divide.
Logical Shifts (Used for Unsigned)
- Left Shift: Move all bits to the left. Fill the empty spots on the right with 0s. This multiplies the number by 2 for every shift.
- Right Shift: Move all bits to the right. This divides the number by 2 for every shift (ignoring any remainder).
Arithmetic Shifts (Used for Signed Numbers)
When you right-shift a signed (Two's Complement) number, you must keep the sign bit (the left-most bit) the same to make sure a negative number stays negative.
Memory Aid: Left is Larger (Multiply). Right is Reduce (Divide).
6. Hexadecimal (Base-16)
Binary is great for computers, but humans find long strings like 11011010 very hard to read. We use Hexadecimal as a "shorthand."
Why use Hex?
- It is easier for humans to read and remember.
- There is less chance of making a mistake when typing it.
- It is very easy to convert between Hex and Binary.
The Hex Values
Hex uses 0-9, then letters for values 10-15:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A(10), B(11), C(12), D(13), E(14), F(15)
Converting Binary to Hex
1. Split your 8-bit binary pattern into two 4-bit groups (called nibbles).
2. Calculate the denary value of each nibble (0-15).
3. Convert that value to its Hex character.
Example: 1010 1100
1010 = 10 (A in Hex)
1100 = 12 (C in Hex)
Result = AC
Quick Review: One Hex digit represents exactly 4 bits. Two Hex digits represent 8 bits (1 byte).
Key Takeaway: Hexadecimal is a human-friendly way of representing binary. It doesn't change how the computer works; it just makes life easier for programmers!