OCR A Level Computer Science: 4.1 Data Types
OCR A Level Computer Science: 4.1 Data Types
Page 1 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Casting
Casting is when you convert one data type to another data type
When a user enters data into a program, this will more than likely be in a string format
It's essential to convert some of this string data to a numerical format where possible
For example, you may want to perform calculations on age-related data to determine if someone is
eligible to vote
Page 2 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Some programming languages can't execute numerical comparisons on text data, making this
transformation crucial
Your notes
For example if you had "12" stored as a string and you wanted to know if this value was below 20
Therefore, the string value of "12" will need to be cast as an integer to allow the comparison to take
place
Python example
int_value = int("123") // converts the string "123" to 123
Java example
int intValue = Integer.parseInt("123"); // converts the string "123" to 123
real_value = float(int_value)
int_value = int(real_value)
int_value = int(int_str)
str_value = str(value)
Page 3 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
str_val = str(bool_val)
bool_val = bool(bool_str)
WORKED EXAMPLE
You are tasked with designing a program for a bookstore.
The program should be able to store each piece of information given in table.
Complete the table by identifying a suitable data type for each piece of information in the table.
5 marks
Answer:
Answer that gets full marks
Page 4 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 5 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Bits
A bit is the smallest unit of digital information, representing either an "off" (0) or an "on" (1) state.
Bytes
Bits are grouped into larger structures to form bytes (8 bits), words, and long words
These groupings allow us to represent more complex information, like numbers, text, and instructions
Page 6 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Page 7 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Abstraction layers
Computers handle large volumes of basic numeric data
To create meaningful representations of data, layers of abstraction exist so that basic data can be
interpreted upwards into other forms, e.g. images, sound, video
The same principle applies to programming languages that compile down into binary code
At the bottom, you have binary, and each layer above it allows for more meaningful information to be
represented
Page 8 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Abstractions of binary
Page 9 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
128 64 32 16 8 4 2 1
Then we start at the left and look for the highest number that is less than or equal to 101 and if so, place
a 1 in that column. Otherwise, place a 0 in the column
128 is bigger than 101 and therefore we place a 0 in that column
64 is smaller than 101 so we place a 1 in that column. 101 - 64 = 37. This now means we have 37 left to find
32 is smaller than 37 so we place a 1 in that column. 37 - 32 = 5. This now means we have 5 left to find
16 is bigger than 5 and therefore we place a 0 in that column
8 is bigger than 5 and therefore we place a 0 in that column
4 is smaller than 5 so we place a 1 in that column. 5 - 1 = 1. This now means we have 1 left to find
Page 10 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
0 1 1 0 0 1 0 1
Page 11 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 12 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Page 13 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
MSB 64 32 16 8 4 2 1
1 1 0 0 0 1 1 1
64 + 4 + 2 + 1 = 71
Apply a sign of 1 to make -71
Therefore the denary number -71 in binary is 11000111
WORKED EXAMPLE
Page 14 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Convert the 8-bit sign and magnitude binary number 10001011 to denary.
How to answer this question: Your notes
Identify the sign bit: The MSB is 1, so the number is negative
Isolate the magnitude: We are left with 0001011 by removing the sign bit
Convert to denary: The binary number 0001011 converts to 11 in denary
Apply the sign: The MSB was 1, so the number is -11 in denary
Answer:
Answer that gets full marks
10001011 converts to -11 in denary
Two's Complement
Two's complement is a different method for representing negative binary numbers
Calculations on two's complement numbers are less computationally intensive
Method
1. Start with the absolute value of the number, in this case 12
2. Invert the bits so that all of the 1's become 0's and all of the 0's become 1's
3. Add 1
Page 15 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
-128 64 32 16 8 4 2 1
0 0 0 1 1 0 0 0
-128 64 32 16 8 4 2 1
1 1 1 0 0 1 1 1
-128 64 32 16 8 4 2 1
1 1 1 0 1 0 0 0
Page 16 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Example addition
Overflow Errors
Overflow occurs when the sum of two binary numbers exceeds the given number of bits
Page 17 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
In signed number representations, the leftmost bit often serves as the sign bit; overflow can flip this,
incorrectly changing the sign of the result
Your notes
Overflow generally leads to incorrect or unpredictable results as the extra bits are truncated or
wrapped around
Binary Subtraction
As well as adding binary numbers, we can also subtract binary numbers
One method of doing this is to use two's complement
Example 1
Subtract 1001 (9) from 0011 (3)
1. Given numbers
Number 1 1 0 0 1
Number 2 0 0 1 1
2. Two's complement
Convert the number to subtract (0011) to its two's complement
Invert bits to get 1100
Add 1 to get 1101
Number 1 1 0 0 1
Page 18 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Number 2 (Converted) 1 1 0 1
Your notes
3. Addition Operation
Now add 1001 and 1101
1001 + 1101 = (1) 0110 - including an overflow
Carry 1 1
Number 1 1 0 0 1
Number 2 1 1 0 1
Addition 1 0 1 1 0
4. Remove Overflow
The result is 10110 with overflow
Remove the overflow bit to get 0110
In two's complement arithmetic, the overflow bit does not contribute to the actual value of the
operation but is more of a by-product of the method.
Page 19 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Hexadecimal Numbers
Your notes
Representing Hexadecimal Numbers
What is Hexadecimal?
It serves as a more human-friendly representation of binary-coded values
Another numbering system is hexadecimal (base-16)
It consists of 10 numbers (0-9) and 6 letters (A-F)
Page 20 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Sample hexcolours
Page 21 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
When the denary scale reaches 10, this is when the hexadecimal scale switches to letters, starting with
A
Your notes
Denary Binary Hexadecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
Page 22 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
15 1111 F
Your notes
Using subscript in number representation
The subscript in number representation denotes the base of a number
It helps in differentiating between number systems
Common uses:
10 2 indicates the number is in binary (base 2), and its value is '10' in binary
10 10 indicates the number is in denary (base 10), and its value is '10' in denary
10 16 indicates the number is in hexadecimal (base 16), and its value is '10' in hex, equivalent to '16'
in denary
It provides clarity, especially in contexts where multiple numbering systems are discussed
Denary to Hexadecimal
Convert the denary number 241 to hexadecimal.
Step 1: Convert the number to binary
Page 23 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Hexadecimal to Denary
Convert the hexadecimal value 1A to denary.
Step 1: Convert each hexadecimal character into binary
Binary to Hexadecimal
Convert 1101 0101 to hexadecimal.
Step 1: Take the binary values
Page 24 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Hexadecimal to Binary
Convert B2 from Hexadecimal to Binary.
Step 1: Split the Hexadecimal into two separate digits and convert each to a nibble
Page 25 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 26 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
-8 4 2 1 . 0.5 0.25
0 1 1 0 . 1 1
0 . 1 1 0 1 1
-4 2 1
0 1 1
Page 27 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
0 . 1 1 0 0
-4 2 1
0 1 1
-8 4 2 1 . 0.5
0 1 1 0 . 0
Why normalise?
Ensures a consistent format for floating point representation
Makes arithmetic and comparisons more straightforward
Page 28 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Moving the point to the left increases the exponent and vice versa.
Example
Your notes
Before normalisation:
Mantissa = 0.0011
Exponent = 0010 (2)
After normalisation:
Mantissa = 0.1100 – Decimal point has moved 2 places to right so it starts with 01
Exponent = 0000 (0) - Exponent has been reduced by 2
Page 29 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Number A: 1.101×23
Number B: 1.010×22
Number A has an exponent of 23 and B has an exponent of 22 , we need to adjust B to have the
same exponent as A
This is achieved by moving the point one space to the left in Number B and increasing the
exponent by 1
Page 30 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
4. Determine Sign
For addition: If both numbers are positive or negative, the result takes the common sign
If they have different signs, the result's sign depends on the larger absolute value
For subtraction: The sign is determined by the sign of the number you're subtracting from and the
result of the subtraction
Example addition
1. 1.100 1 2 ×2 3 + 1. 011 0 2 ×2 2
Example subtraction
1. 1.100 1 2 ×2 3 - 1. 011 0 2 ×2 2
Page 31 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Logical Shifts
Logical binary shifts are operations performed on binary numbers where all the bits in the number are
moved left or right by a specified number of positions
These shifts are commonly used in computer programming and digital systems
There are two types of logical binary shifts: Left and Right
Page 32 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Bitwise Manipulation
Bitwise AND operation
If both bits are 1 in the binary number and the mask, the result will be 1. Otherwise, the result will be 0.
Description 128 64 32 16 8 4 2 1
Binary 1 0 1 1 1 0 0 1
Mask 0 0 1 1 0 0 0 0
Result 0 0 1 1 0 0 0 0
Bitwise OR operation
If either bit is 1 in the binary number or the mask, the result will be 1. Otherwise, the result will be 0.
Description 128 64 32 16 8 4 2 1
Page 33 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Binary 1 1 0 0 1 0 1 0
Your notes
Mask 0 1 1 1 0 0 0 0
Result 1 1 1 1 1 0 1 0
Description 128 64 32 16 8 4 2 1
Binary 1 0 1 0 1 0 1 0
Mask 0 0 1 1 0 0 0 0
Result 1 0 0 1 1 0 1 0
Page 34 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Character Sets
Your notes
Character Sets
How are characters represented?
Computers only understand binary and therefore we need to represent characters using binary codes
For example, the letter 'A' might be represented as 01000001 in binary
Character sets
A character set is a list of all of the characters and their associated binary code
Character sets standardise the binary codes for each character
Without a character set, one system might interpret 01000001 differently from another
Two common character sets are:
American Standard Code for Information Interchange (ASCII)
UNICODE
ASCII
ASCII uses 7-bits to encode each character, providing for 128 distinct characters
For example, 'A' is represented as 65 in decimal, which is 1000001 in binary
ASCII was created to provide a common standard for encoding characters, which was necessary for
compatibility among various types of hardware and software
An extended version of ASCII exists which encodes each character using 8-bits creating 256
characters
ASCII table
The ASCII table shows the relationship between characters that humans recognise and the denary
values that represent them in the system
The denary values can then be converted to binary, representing the original character as binary
Page 35 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
ASCII Table
Limitations of ASCII
1. It has a limited number of characters
ASCII is limited to 128 characters, which include English alphabets, numerals, and some special and control
characters.
A, B, C, ..., Z
a, b, c, ..., z
0, 1, ..., 9
!, @, #, ...
Page 36 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
ASCII cannot represent characters from languages other than English, limiting its applicability globally.
No representation for: 'α', 'ö', 'ñ', Your notes
3. There is no provision for modern symbols
ASCII does not include modern symbols or emoji's common in today's digital communication.
Unicode
UNICODE was created to be a solution to the limitations of ASCII
UNICODE uses a much larger bit range, up to 32-bits (depending on the encoding method), allowing
for a wide variety of characters from different languages and scripts
Example: The Greek character Lambda 'λ' is represented as U+03BB
U+03BB breaks down to:
U+, meaning this is a Unicode character
03BB, meaning character 03BB in the UNICODE set
Impact on storage
ASCII is more storage-efficient, with characters requiring only 7-bits
UNICODE characters can require up to 32-bits, thus potentially using more storage space
Comparison
ASCII UNICODE
Uses Used to represent characters in the Used to represent characters across the
English language. world.
Page 37 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Benefits It uses a lot less storage space than It can represent more characters than
UNICODE. ASCII.
Your notes
It can support all common characters
across the world.
It can represent special characters such
as emoji's.
Drawbacks It can only represent 128 characters. It uses a lot more storage space than
ASCII.
It cannot store special characters such
as emoji's.
Page 38 of 38
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers