1.4.1 Data Types
1.4.1(a) Primitive Data Types
Understanding data types is fundamental to programming. Different types of data are stored and processed differently by computers.
Integer
NOT integers: 3.14, "42", 5.0
Real (Floating Point)
Boolean
hasPermission = False
isAdult = age >= 18 // Result is True or False
Character
Each character has a numeric code (ASCII/Unicode)
String
empty = ""
number = "42" // This is a STRING, not an integer!
• Description: Whole numbers
• Example: 42, -7, 0
Real/Float
• Description: Decimal numbers
• Example: 3.14, -0.5
Boolean
• Description: True or False
• Example: True, False
Character
• Description: Single symbol
• Example: 'A', '7'
String
• Description: Text
• Example: "Hello"
1.4.1(b) Binary Representation of Positive Integers
Computers store all data as binary (base-2) - patterns of 0s and 1s.
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value (2^n) | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Convert 10110101 to denary:
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 |
Answer: 10110101 in binary = 181 in denary
Convert 157 to binary:
Method: Repeatedly divide by 2, note remainders.
| Division step | Quotient | Remainder |
|---|---|---|
| 157 ÷ 2 | 78 | 1 |
| 78 ÷ 2 | 39 | 0 |
| 39 ÷ 2 | 19 | 1 |
| 19 ÷ 2 | 9 | 1 |
| 9 ÷ 2 | 4 | 1 |
| 4 ÷ 2 | 2 | 0 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
Answer: 157 in denary = 10011101 in binary
1.4.1(c) Binary Representation of Negative Numbers
Sign and Magnitude
+5 = 00000101
-5 = 10000101
Problem: Two representations of zero (00000000 and 10000000)
Two's Complement
Represent -42 in 8-bit two's complement:
Step 1: Write +42 in binary
42 = 00101010
Step 2: Starting from the right, find the first 1 — keep everything from there rightward the same, flip everything to the left.
| Original | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Action | flip | flip | flip | flip | flip | flip | keep | keep |
| Result | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 |
Answer: -42 in two's complement = 11010110
What decimal number does 11110011 represent?
Step 1: MSB is 1, so it's negative.
Step 2: To find the magnitude, convert back — keep everything up to and including the first 1 from the right, flip the rest.
| Original | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
|---|---|---|---|---|---|---|---|---|
| Action | flip | flip | flip | flip | flip | flip | keep | keep |
| Result | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 |
Answer: 11110011 = -13
• Minimum: -128 (10000000)
• Maximum: +127 (01111111)
• One more negative number than positive!
1.4.1(d) Binary Addition and Subtraction
Binary Addition Rules
| Sum | Result bit | Carry out |
|---|---|---|
| 0 + 0 | 0 | 0 |
| 0 + 1 | 1 | 0 |
| 1 + 0 | 1 | 0 |
| 1 + 1 | 0 | 1 |
| 1 + 1 + 1 | 1 | 1 |
Add 01101011 + 00110101
| Decimal check | Value | |||||||
|---|---|---|---|---|---|---|---|---|
| 01101011 | 107 | |||||||
| 00110101 | 53 | |||||||
| Total | 160 | |||||||
| Row | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Carry | 1 | 1 | 1 | 0 | 0 | 1 | 1 | |
| A | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 1 |
| B | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 1 |
| Sum | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
• Bit 0: 1+1 = 10 → write 0, carry 1
• Bit 1: 1+0+1(carry) = 10 → write 0, carry 1
• Bit 2: 0+1+1(carry) = 10 → write 0, carry 1
• Bit 3: 1+0+0 = 1 → write 1, no carry
• Bit 4: 0+1+0 = 1 → write 0... (continue)
• Bit 5: 1+1+0 = 10 → write 0, carry 1
• Bit 6: 1+0+1(carry) = 10 → write 0, carry 1
• Bit 7: 0+0+1(carry) = 1 → write 1
Answer: 01101011 + 00110101 = 10100000 (107 + 53 = 160)
Binary Subtraction Using Two's Complement
Calculate 50 - 30 using two's complement (i.e. 50 + (-30)).
| Step | Working |
|---|---|
| Convert 50 to 8-bit binary | 00110010 |
| Convert 30 to 8-bit binary | 00011110 |
| Two's complement of 30 | Keep from right up to and including first 1 ("10"), flip the rest → 11100010 |
| Row | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|
| Carry | 1 | 1 | 1 | 0 | 0 | 0 | 1 | ||
| A | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | |
| B | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 | |
| Sum | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 |
Answer: 50 - 30 = 20 ✓
1.4.1(e-f) Hexadecimal
| Hex | Decimal | 4-bit Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
| Conversion | Working | Answer |
|---|---|---|
| Binary to Hex | 11010110 → 1101 0110 → D 6 | D6 |
| Hex to Binary | 4A → 4 = 0100, A = 1010 | 01001010 |
| Conversion | Working | Answer |
|---|---|---|
| Hex to Denary | 2F = (2 × 16^1) + (15 × 16^0) = 32 + 15 | 47 |
| Denary to Hex | 200 ÷ 16 = 12 remainder 8; 12 = C | C8 |
1.4.1(g) Floating Point Numbers
• Sign bit: 0 = positive, 1 = negative
• Mantissa: The significant digits
• Exponent: Power of 2 to multiply by
Trade-off:
• More mantissa bits = more precision
• More exponent bits = larger range
A normalised floating point number has its mantissa starting with 01 (positive) or 10 (negative). This ensures maximum precision.
1.4.1(h) Character Sets
ASCII
• 7 bits = 128 characters
• Covers English letters, digits, punctuation, control characters
• 'A' = 65, 'a' = 97, '0' = 48
• Limited - only English characters
Unicode
• Supports over 140,000 characters
• Covers virtually all world languages
• Includes emojis, mathematical symbols, historic scripts
• UTF-8: Variable length (1-4 bytes per character)
• Backward compatible with ASCII
| Character set | Bits | Characters | Scope |
|---|---|---|---|
| ASCII | 7 | 128 | English only |
| Extended ASCII | 8 | 256 | English + European |
| Unicode (UTF-8) | 8-32 | 140,000+ | All languages + emoji |
• Global internet requires all languages
• ASCII cannot represent Chinese, Arabic, Hindi, etc.
• Emojis need Unicode
• UTF-8 is efficient - uses 1 byte for ASCII characters