Welcome to the World of Data!

Ever wondered how a computer knows the difference between a number you want to add, a letter in your name, or a "True/False" answer in a quiz? Since computers only really understand electricity (On or Off), we use Data Types to tell the computer how to interpret those 1s and 0s. In this chapter, we’ll look at the "building blocks" of data and how computers handle numbers, text, and logic. Don't worry if it seems like a lot of math at first—once you see the patterns, it’s like learning a secret code!

1. The "Basics": Primitive Data Types

Before we dive into binary, let's look at the labels we give data. Think of these like different shaped containers; you wouldn't put soup in a sieve, and you wouldn't store a name in a "number" box.

The "Big Five" you need to know:

  • Integer: A whole number (e.g., 7, -12, 1066). No decimals allowed!
  • Real (or Floating Point): Numbers with a fractional part or decimal point (e.g., 3.14, -0.5, 10.0).
  • Character: A single letter, number, or symbol (e.g., 'A', '!', '3').
  • String: A collection of characters, usually used for text (e.g., "Hello World", "Computer Science").
  • Boolean: Can only be one of two values: True or False (often used for switches or logic).

Quick Review: Which data type would you use for a price? Real. What about the number of students in a class? Integer.

2. Number Systems: Binary and Hexadecimal

We use Denary (Base 10) in everyday life. Computers use Binary (Base 2). Sometimes, humans use Hexadecimal (Base 16) as a shorthand because binary gets very long and confusing!

Binary (Base 2)

Binary uses only two digits: 0 and 1. Each "place" in binary is a power of 2.

Example: The binary number 1011
\( (1 \times 8) + (0 \times 4) + (1 \times 2) + (1 \times 1) = 11 \) in denary.

Hexadecimal (Base 16)

Hex uses 0-9 and then A, B, C, D, E, F for values 10-15. It is much easier for humans to read 'FF' than '11111111'!

How to convert Hex to Binary:
Take each Hex digit and turn it into a 4-bit binary "nibble".
Example: Hex 'A3'
A = 10 = 1010 in binary
3 = 3 = 0011 in binary
So, A3 = 10100011

Did you know? Hexadecimal is often used for HTML color codes (like #FF0000 for red) and MAC addresses.

Takeaway: Binary is for computers; Hexadecimal is a human-friendly shorthand for binary; Denary is for us.

3. Binary Arithmetic: Adding and Subtracting

Adding binary is just like adding denary, but you "carry over" much sooner!

Addition Rules:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 1 = 0 (carry 1)
  • 1 + 1 + 1 = 1 (carry 1)

Common Mistake: Forgetting to carry the 1 can ruin your whole calculation. Always write your carries clearly above the next column!

Subtraction:

In the OCR syllabus, we usually subtract by adding a negative number. To do this, we need to know how to represent negative numbers first.

4. Negative Numbers: Sign & Magnitude and Two's Complement

Since we don't have a "-" symbol in binary, we have to use a bit to represent the sign.

Sign and Magnitude

The most significant bit (the one on the far left) is the Sign Bit.
0 = Positive (+)
1 = Negative (-)
Example (8-bit): 00000101 is +5. 10000101 is -5.

Two's Complement

This is the preferred way because it makes math easier for the CPU. To turn a positive number into a negative one:
1. Start with the positive binary number.
2. Flip all the bits (0 becomes 1, 1 becomes 0).
3. Add 1 to the result.

Memory Aid: "Flip and Add One" to go from positive to negative!

Key Takeaway: Two's complement is better because it allows the computer to subtract using the same hardware it uses for addition.

5. Real Numbers: Floating Point Representation

How do we store 12.5 in binary? We use something similar to scientific notation (like \(1.25 \times 10^1\)). In binary, this is split into two parts: the Mantissa and the Exponent.

The Formula: \( \text{Number} = \text{Mantissa} \times 2^{\text{Exponent}} \)

Normalisation

To ensure we are as accurate as possible, we normalise numbers. A normalised positive number always starts with 01. A normalised negative number always starts with 10.

Don't worry if this seems tricky at first: Just remember that the Mantissa gives you the "digits" and the Exponent tells you where to move the binary point. A positive exponent moves the point to the right; a negative exponent moves it to the left.

Quick Review: Normalisation is done to provide the maximum precision for a given number of bits.

6. Character Sets: ASCII and Unicode

A character set is a giant "lookup table" that links a unique binary number to a character.

  • ASCII: Uses 7 or 8 bits. It can represent 128 (or 256) characters. Great for English, but doesn't have enough room for symbols like emojis or different languages like Chinese or Arabic.
  • Unicode: Uses 16 or 32 bits. It can represent millions of characters! It includes every language and even the 🍕 emoji.

The Trade-off: Unicode can represent more characters, but it takes up more storage space per character than ASCII does.

Did you know? The first 128 codes in Unicode are exactly the same as ASCII to keep them compatible!

Final Chapter Summary

  • Primitive Types: The basic labels for data (Integer, Real, etc.).
  • Binary/Hex: How we represent numbers in base 2 and base 16.
  • Two's Complement: The smartest way to handle negative numbers.
  • Floating Point: How we handle decimals using Mantissa and Exponent.
  • Character Sets: ASCII for basics, Unicode for everything else.