Welcome to the World of Binary!

Ever wondered how a computer, which is basically a collection of switches and wires, can show you high-definition movies or play complex video games? It all comes down to Binary. In this chapter, we’ll explore the "language" of computers. Don't worry if it seems like a lot of 1s and 0s right now—by the end of these notes, you'll be reading and calculating like a machine!

1. The Basics: Number Bases

Before we dive into binary, let's look at how we normally count. We use Base 10 (Decimal/Denary), likely because we have ten fingers! Computers use Base 2 (Binary) because they are made of transistors that are either On (1) or Off (0).

Key Number Systems

  • Decimal (Base 10): Uses digits 0-9.
  • Binary (Base 2): Uses digits 0 and 1.
  • Hexadecimal (Base 16): Uses 0-9 and A-F. It’s used by humans as a "shorthand" for binary because it’s much easier to read. For example, the binary 10101110 is just AE in Hex!

Did you know? A single binary digit (a 0 or a 1) is called a bit. If you group 8 bits together, you get a byte!

Quick Review: Powers of 2

To master binary, you should know these numbers by heart:
\( 2^0=1 \), \( 2^1=2 \), \( 2^2=4 \), \( 2^3=8 \), \( 2^4=16 \), \( 2^5=32 \), \( 2^6=64 \), \( 2^7=128 \).

Summary:

Key Takeaway: Number bases are just different ways of representing the same value. Binary is for computers, Decimal is for humans, and Hexadecimal is the bridge between them.


2. Unsigned Binary

Unsigned binary is used to represent positive integers only. There is no way to show a minus sign here.

Range of Values

If you have \( n \) bits, you can represent \( 2^n \) different values.
The minimum value is always 0.
The maximum value is \( 2^n - 1 \).

Example: With 8 bits (a byte), you can represent numbers from 0 up to 255 (\( 2^8 - 1 \)).

Binary Arithmetic

Addition

It’s just like normal column addition! Just remember these four rules:
1. \( 0 + 0 = 0 \)
2. \( 0 + 1 = 1 \)
3. \( 1 + 1 = 0 \) (carry 1 to the next column)
4. \( 1 + 1 + 1 = 1 \) (carry 1 to the next column)

Multiplication

To multiply unsigned binary numbers, we use the "shift and add" method, similar to long multiplication in school. If you multiply by 2, you are simply shifting all bits one place to the left and adding a 0 at the end!

Common Mistake: Forgetting the "carry" in addition. Always write your carries clearly above the next column so you don't lose them!

Summary:

Key Takeaway: Unsigned binary is for positive numbers. The more bits you have, the larger the number you can store.


3. Signed Binary: Two's Complement

How do we tell a computer a number is negative? We use a system called Two's Complement.

The Concept

In an 8-bit Two's Complement number, the most significant bit (the one on the far left) has a negative weight. Instead of representing 128, it represents -128.

The "Flip and One" Trick

To convert a positive number to a negative number (e.g., \( 5 \) to \( -5 \)):
1. Write out the positive number in binary (e.g., 00000101).
2. Flip all the bits (0 becomes 1, 1 becomes 0) -> 11111010.
3. Add 1 to the result -> 11111011. This is \( -5 \)!

Subtraction

Computers don't actually "subtract"—they just add negative numbers! To do \( 10 - 5 \), the computer calculates \( 10 + (-5) \) using Two's Complement.

Summary:

Key Takeaway: Two's Complement lets us represent both positive and negative numbers. If the leftmost bit is 1, the number is negative!


4. Fractional Numbers

Life isn't all whole numbers. Sometimes we need decimals (like 10.5). Computers use two main methods: Fixed Point and Floating Point.

Fixed Point

We imagine a binary point at a set position. For example, in an 8-bit number, we might say the first 4 bits are whole numbers and the last 4 bits are fractions (\( 1/2, 1/4, 1/8, 1/16 \)).
Pros: Fast for the computer to process.
Cons: Limited range; you can't represent very large and very small numbers at the same time.

Floating Point

This is like "Scientific Notation" (e.g., \( 6.02 \times 10^{23} \)). It consists of two parts:
1. Mantissa: The actual digits of the number.
2. Exponent: Tells us where the binary point moves.

A positive exponent moves the point to the right (makes it bigger).
A negative exponent moves the point to the left (makes it smaller).

Summary:

Key Takeaway: Fixed point is simple but rigid. Floating point is complex but allows for a huge range of very big or very tiny numbers.


5. Normalisation and Errors

In your exams, you'll often be asked to normalise a floating point number. This just means adjusting the mantissa and exponent so the number is as precise as possible.

How to spot a Normalised Number:

  • A positive normalised number always starts with 01.
  • A negative normalised number always starts with 10.

Rounding Errors

Don't worry if this seems tricky: Some decimal numbers (like \( 0.1 \)) can never be represented perfectly in binary, no matter how many bits you use. It’s like trying to write \( 1/3 \) as a decimal (\( 0.3333... \))—it goes on forever! This leads to tiny rounding errors.

Absolute and Relative Error

  • Absolute Error: The actual difference between the real number and our binary version.
  • Relative Error: The absolute error divided by the real number (often shown as a percentage).

Underflow and Overflow

  • Overflow: When a number is too large to fit in the bits provided (like trying to fit a gallon of water into a pint glass).
  • Underflow: When a number is too small to be represented (like trying to measure a single atom with a meter ruler).
Summary:

Key Takeaway: Normalisation ensures maximum precision. However, binary will always have limits, leading to errors, overflow, or underflow.


6. Units of Information

Finally, we need to know how to measure data. You might think a Kilobyte is 1000 bytes, but in Computer Science, it's a bit more specific!

Binary Prefixes (Powers of 2)

These are the ones computers actually use:

  • Kibi (Ki): \( 2^{10} \) (1,024 bytes)
  • Mebi (Mi): \( 2^{20} \)
  • Gibi (Gi): \( 2^{30} \)
  • Tebi (Ti): \( 2^{40} \)

Decimal Prefixes (Powers of 10)

These are the ones humans usually use (and hard drive manufacturers often use to make their drives look bigger!):

  • Kilo (k): \( 10^3 \) (1,000 bytes)
  • Mega (M): \( 10^6 \)
  • Giga (G): \( 10^9 \)
  • Tera (T): \( 10^{12} \)

Memory Aid: If it has an "i" in the name (like Kibi), it's the intelligent computer version based on 1024!

Summary:

Key Takeaway: Always check if an exam question asks for Decimal (1000s) or Binary (1024s) prefixes. It makes a big difference in your final answer!