Welcome to the World of Data Types!
Hello! Today we are diving into the "atoms" of Computer Science: Data Types. Just like you can't cook a meal without knowing whether your ingredients are liquid or solid, a computer can't process information without knowing exactly what kind of data it's looking at.
In this guide, we will explore how computers store numbers, text, and logic. Don't worry if some of the binary math looks like "The Matrix" at first—we'll break it down step-by-step!
1. The "Big Five" Primitive Data Types
A primitive data type is a basic building block provided by a programming language. Think of these as different shaped boxes that can only hold one specific type of item.
- Integer: Whole numbers with no fractional part (e.g., -5, 0, 42).
- Real (or Floating Point): Numbers with a fractional/decimal part (e.g., 3.14, -0.01).
- Character: A single letter, digit, or symbol (e.g., 'A', '!', '3').
- String: A collection of characters, usually used for words or sentences (e.g., "Hello World").
- Boolean: Can only be one of two values: True or False.
Quick Review: If you were storing someone's age, you'd use an Integer. If you were storing their height in meters, you'd use a Real number!
2. Representing Positive Integers
Computers use Binary (Base 2), which only uses 0s and 1s. This is because the hardware is made of tiny switches that are either OFF (0) or ON (1).
Denary to Binary Conversion
To convert a normal number (Denary) to binary, we use placeholders that double each time: 128, 64, 32, 16, 8, 4, 2, 1.
Example: Convert 13 to binary.
1. Does 8 fit into 13? Yes. (13 - 8 = 5). Place a 1 under the 8.
2. Does 4 fit into 5? Yes. (5 - 4 = 1). Place a 1 under the 4.
3. Does 2 fit into 1? No. Place a 0 under the 2.
4. Does 1 fit into 1? Yes. Place a 1 under the 1.
Result: \(00001101\)
Hexadecimal (Base 16)
Binary is hard for humans to read. Hexadecimal is a shorthand. It uses 0-9 and then A, B, C, D, E, F for values 10-15.
Why use Hex? One Hex digit represents exactly 4 bits (a nibble). It's much easier to remember #FF0000 than a long string of 0s and 1s!
3. Negative Numbers in Binary
How does a computer know if a number is negative? We have two main methods:
Sign and Magnitude
This is the simplest way. We use the Most Significant Bit (MSB)—the one furthest to the left—as a "flag."
- If the MSB is 0, the number is positive.
- If the MSB is 1, the number is negative.
Example: If \(00001101\) is +13, then \(10001101\) is -13.
The Problem: It's hard for computers to do math this way, and we end up with "negative zero" (\(10000000\)), which is confusing!
Two's Complement
This is the "pro" way. The MSB has a negative value. In an 8-bit number, the leftmost bit is -128 instead of +128.
The "Flip and Add 1" Trick:
To turn +5 into -5:
1. Start with +5: \(00000101\)
2. Flip all bits (NOT): \(11111010\)
3. Add 1: \(11111011\)
Now, if you add up the values: \(-128 + 64 + 32 + 16 + 8 + 2 + 1 = -5\). It works!
4. Binary Arithmetic
Adding binary is just like adding normal numbers, but you "carry over" when you hit 2.
Rules of Addition:
- \(0 + 0 = 0\)
- \(0 + 1 = 1\)
- \(1 + 1 = 0\) (carry 1)
- \(1 + 1 + 1 = 1\) (carry 1)
Common Mistake: Overflow!
If you add two numbers and the result needs 9 bits but you only have an 8-bit "box," the extra bit is lost. This is an Overflow Error, and it can cause programs to crash or behave strangely.
5. Floating Point Numbers (Decimals)
In A Level Computer Science, we represent decimals using a Mantissa and an Exponent.
Think of it like scientific notation: \(Mantissa \times 2^{Exponent}\).
Normalization
To ensure we have the most precision possible and only one way to represent a number, we normalize it.
- A normalized positive number always starts with 01.
- A normalized negative number always starts with 10.
Analogy: Normalization is like saying we should always write "1.23 x 10²" instead of "12.3 x 10¹" or "0.123 x 10³". It keeps things tidy!
6. Bitwise Manipulation
Sometimes we want to look at individual bits. We use Logical Masks for this.
- AND: Used to extract or clear bits. (Both must be 1 to get a 1).
- OR: Used to set bits (force them to be 1). (Either can be 1 to get a 1).
- XOR: Used to flip bits. (Must be different to get a 1).
Bitwise Shifts
- Logical Shift Left: Moves bits left and adds 0 at the end. Effect: Multiplies by 2.
- Logical Shift Right: Moves bits right. Effect: Divides by 2 (removes the remainder).
7. Character Sets: ASCII and Unicode
How does \(01000001\) become the letter 'A'? We use a Character Set, which is a lookup table.
ASCII:
- Uses 7 or 8 bits.
- Can represent 128 or 256 characters.
- Enough for English, but not for much else.
Unicode:
- Uses 16 or 32 bits.
- Can represent millions of characters.
- Includes all languages, scientific symbols, and even Emojis! 🍕
Key Takeaway: Unicode is the modern standard because it is global, whereas ASCII is very limited.
Summary Checklist
Before you move on, make sure you can:
- [ ] List the 5 primitive data types.
- [ ] Convert between Denary, Binary, and Hex.
- [ ] Explain why Two's Complement is better than Sign and Magnitude.
- [ ] Perform a binary addition and identify an overflow.
- [ ] Describe the purpose of a Mantissa and Exponent.
- [ ] State the difference between ASCII and Unicode.
Don't worry if the floating point math feels heavy right now. Practice drawing out the placeholders, and it will become second nature! You've got this!