H446/01 · Computer SystemsSection 1.4

1.4.1 Data types

Primitive data types, binary representation of integers (two's complement), fixed and floating point, character sets (ASCII, Unicode), images and sound representation

📚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.

Definition
Primitive data types are the basic building blocks for storing data in programs. They define what kind of data a variable can hold and what operations can be performed on it.

Integer

Definition
An integer is a whole number, positive or negative, with no decimal point.

Example
Valid integers: 42, -17, 0, 1000000
NOT integers: 3.14, "42", 5.0

Real (Floating Point)

Definition
A real number (or float) is a number that can have a fractional part, represented with a decimal point.

Example
Valid reals: 3.14, -0.5, 100.0, 2.71828

Boolean

Definition
A Boolean can only have two values: True or False. Used for logical operations and conditions.

Example
isLoggedIn = True
hasPermission = False
isAdult = age >= 18 // Result is True or False

Character

Definition
A character is a single symbol - a letter, digit, punctuation mark, or special symbol.

Example
Valid characters: 'A', 'z', '7', '!', ' '
Each character has a numeric code (ASCII/Unicode)

String

Definition
A string is a sequence of characters, used to represent text.

Example
name = "Alice Smith"
empty = ""
number = "42" // This is a STRING, not an integer!

Key Point
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.

Definition
Binary is a number system using only two digits: 0 and 1. Each digit position represents a power of 2.

Key Point
Place Values (8-bit):

Bit position76543210
Value (2^n)1286432168421

Worked Example
Worked Example: Binary to Denary Conversion

Convert 10110101 to denary:

Value1286432168421
Bit10110101
Calculation: 128 + 32 + 16 + 4 + 1 = 181

Answer: 10110101 in binary = 181 in denary

Worked Example
Worked Example: Denary to Binary Conversion

Convert 157 to binary:

Method: Repeatedly divide by 2, note remainders.

Division stepQuotientRemainder
157 ÷ 2781
78 ÷ 2390
39 ÷ 2191
19 ÷ 291
9 ÷ 241
4 ÷ 220
2 ÷ 210
1 ÷ 201
Read remainders bottom to top: 10011101

Answer: 157 in denary = 10011101 in binary


1.4.1(c) Binary Representation of Negative Numbers

Sign and Magnitude

Definition
In sign and magnitude, the leftmost bit indicates the sign (0 = positive, 1 = negative) and the remaining bits represent the value.

Example
8-bit sign and magnitude:
+5 = 00000101
-5 = 10000101

Problem: Two representations of zero (00000000 and 10000000)

Two's Complement

Definition
Two's complement is the standard method for representing negative numbers in binary. To negate a number: starting from the right, keep everything up to and including the first 1 the same, then flip all the remaining bits to the left.

Worked Example
Worked Example: Converting to 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.

Original00101010
Actionflipflipflipflipflipflipkeepkeep
Result11010110
The first 1 from the right is at position 1 (the "10" at the end). Keep "10", flip the rest.

Answer: -42 in two's complement = 11010110

Worked Example
Worked Example: Reading Two's Complement

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.

Original11110011
Actionflipflipflipflipflipflipkeepkeep
Result00001101
00001101 = 13

Answer: 11110011 = -13

Key Point
Two's Complement Range (8-bit):
• Minimum: -128 (10000000)
• Maximum: +127 (01111111)
• One more negative number than positive!


1.4.1(d) Binary Addition and Subtraction

Binary Addition Rules

Key Point
Binary Addition Rules:

SumResult bitCarry out
0 + 000
0 + 110
1 + 010
1 + 101
1 + 1 + 111

Worked Example
Worked Example: Binary Addition

Add 01101011 + 00110101

Decimal checkValue
01101011107
0011010153
Total160
Row76543210
---------------------------
Carry1110011
A01101011
B00110101
Sum10100000
Working right to left:
• 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

Worked Example
Worked Example: Subtraction

Calculate 50 - 30 using two's complement (i.e. 50 + (-30)).

StepWorking
Convert 50 to 8-bit binary00110010
Convert 30 to 8-bit binary00011110
Two's complement of 30Keep from right up to and including first 1 ("10"), flip the rest → 11100010
Now add 50 + (-30):

Row876543210
Carry1110001
A00110010
B11100010
Sum100010100
Ignore the overflow (bit 8): 00010100 = 20

Answer: 50 - 30 = 20 ✓


1.4.1(e-f) Hexadecimal

Definition
Hexadecimal (hex) is a base-16 number system using digits 0-9 and letters A-F (representing 10-15). It provides a more compact way to represent binary.

Key Point
HexDecimal4-bit Binary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
A101010
B111011
C121100
D131101
E141110
F151111

Worked Example
Worked Example: Binary ↔ Hexadecimal

ConversionWorkingAnswer
Binary to Hex11010110 → 1101 0110 → D 6D6
Hex to Binary4A → 4 = 0100, A = 101001001010

Worked Example
Worked Example: Hex ↔ Denary

ConversionWorkingAnswer
Hex to Denary2F = (2 × 16^1) + (15 × 16^0) = 32 + 1547
Denary to Hex200 ÷ 16 = 12 remainder 8; 12 = CC8


1.4.1(g) Floating Point Numbers

Definition
Floating point is a method of representing real numbers in binary using a mantissa (significant digits) and an exponent (scale factor).

Key Point
Floating Point Structure:
• 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

Key Point
Normalisation:
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

Definition
A character set is a mapping between characters and their numeric codes. It defines which numbers represent which characters.

ASCII

Key Point
ASCII (American Standard Code for Information Interchange):
• 7 bits = 128 characters
• Covers English letters, digits, punctuation, control characters
• 'A' = 65, 'a' = 97, '0' = 48
• Limited - only English characters

Unicode

Key Point
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

Key Point
Character setBitsCharactersScope
ASCII7128English only
Extended ASCII8256English + European
Unicode (UTF-8)8-32140,000+All languages + emoji

Exam Tip
Why Unicode over ASCII?
• 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