A-level Computer Science
Specification Overview
Specification Points
1.4.1 Data Types
• Primitive data types, integer, real/floating point, character, string and Boolean.
• Represent positive integers in binary.
• Use of sign and magnitude and two’s complement to represent negative numbers in binary.
• Addition and subtraction of binary integers.
• Represent positive integers in hexadecimal.
• Convert positive integers between binary, hexadecimal and denary.
• Representation and normalisation of floating point numbers in binary.
• Floating point arithmetic, positive and negative numbers, addition and subtraction.
• Bitwise manipulation and masks: shifts, combining with AND, OR and XOR.
• How character sets (ASCII and UNICODE) are used to represent text.
Overview
Computers use electricity to represent information. An electric current has two possible states: it can either be on or off.
Computers represent ‘on’ using the digit 1 and ‘off’ using the digit 0.
These 0s and 1s are called binary and combinations of 0s and 1s can be used to represent anything from a simple
number to a film.
Each 0 or 1 in binary is called a bit (short for binary digit).
Because it requires so many bits to represent quite small numbers, let alone complex things like games or music files,
groups of bits are commonly grouped together and given names.
A group of 4 bits is called a nibble and can represent 24 (128) combinations. A group of 8 bits is called a byte; a byte
(00000000) can provide 28 (256) different combinations of 0s and 1s.
Bytes are grouped together in the following ways:
• 1 024 bytes = 1 kilobyte (kB) = 210 bytes
• 1 048 576 bytes = 1 megabyte (MB) = 220 bytes
• 1 073 741 824 bytes = 1 gigabyte (GB) = 230 bytes
• 1 099 511 627 776 bytes = 1 terabyte (TB) = 240 bytes
Primitive Data Types
Nearly all programming languages provide
programmers with five primitive (or simple) data
types that can be connected together to create
more complex (or composite) data types. The five
primitive data types are integer, real, character,
string and Boolean.
The amount of memory that each data type takes
up varies from language to language and processor
to processor (they take up more bits on a 64-bit
system compared to a 32-bit system). A good
estimate is that a character takes up 8 bits and an
integer 32 bits.
The rest of this chapter will show how binary is
used to represent all possible values held using
these data types.
Task
Copy this table into your blog and complete:
Field/Variable Data Type Justification for choice of
Data Type
Shoe Size
Password
Multiple Choice Answer (Single Letter)
Age
Over 18?
Goal Difference
Representing Positive Numbers in Binary
Binary is a base 2 number system, which
means that each value is twice as large as
the value before it. Our usual number 1000 100 10 1
system is base 10, also known as denary. 1 0 1 1
In denary, each digit is ten times larger
than the one before it.
In the denary system, 1011 means one
thousand and eleven, because 1000 10 1
equals one thousand and eleven.
8 4 2 1
In the binary system 1011 means eleven,
because 8 + 2 + 1 = 11. 1 0 1 1
Converting from Denary to Binary
98 in denary into binary
98 divide by 2 = 49 remainder 0
49 divide by 2 = 24 remainder 1
24 divide by 2 = 12 remainder 0
12 divide by 2 = 6 remainder 0
6 divide by 2 = 3 remainder 0
3 divide by 2 = 1 remainder 1
1 divide by 2 = 0 remainder 1
0 divide by 2 = 0 remainder 0
01100010
Write the binary value starting from the remainder at the top on the far right hand side.
01100010 which equals 98
Converting from Binary to Denary
10000111 from Binary to Denary
128 64 32 16 8 4 2 1
1 0 0 0 0 1 1 1
128 + 4 + 2 + 1 = 135
Task
Convert the following, show your workings:
1. 01111001 into denary
2. 44 into binary
3. 01011010 in denary
4. 111 into binary
5. 11111001 into denary
Representing negative numbers in binary
Binary can also be used to represent negative numbers (−1, −2, −3 and so
on). There are two ways in which this can be done, you’ll need to read
the exam questions carefully to make sure you use the
correct method.
Sign and Magnitude
Two’s Complement
Sign and Magnitude
Sign and magnitude is the simplest way of representing negative numbers in
binary.
The most significant bit (MSB) is simply used to represent the sign:
1 means the number is negative, 0 means it is positive.
So to represent −4 in 8-bit binary you would write out the 4 in binary,
00000100, then change the MSB to a 1, creating 10000100.
Two’s Complement
Two’s complement is a much more useful way of representing binary numbers
as it allows you to use negative numbers without reducing the range of
numbers you can use.
The easiest way to show a negative number using two’s complement is to
write it out as a positive number using the usual binary method. Then,
starting at the right-hand side, leave every bit up to and including the first 1
alone, but invert all the bits after this.
Two’s Complement
1. For example, to show −90 in binary, first 128 64 32 16 8 4 2 1
work out 90. 90 is 01010110 because 64 +
16 + 8 + 2 = 90. 0 1 0 1 1 0 1 0
1. Then start at the right-hand side and leave 128 64 32 16 8 4 2 1
everything alone up to and including the first
1: 0 1 0 1 1 0 1 0
1. Finally invert everything after this, so all the
(-)128 64 32 16 8 4 2 1
1s become 0s and vice versa:
1 0 1 0 0 1 1 0
2. Thus, −90 in binary is 10100110 in two’s
complement notation.
Binary Addition
Binary addition is fairly straightforward. Just remember these simple
operations:
0 + 0 = 0
1 + 0 = 1
0 + 1 = 1
1 + 1 = 1 0 (1 and 0 / 0 carry 1)
1 + 1 + 1 = 1 1 (1 and 1 / 1 carry 1)
Binary Addition
To add together two binary numbers, just arrange them above each other in
a table. The following table shows the number 59 above the number 117:
1 1 1 1 1 1 1
0 0 1 1 1 0 1 1
0 1 1 1 0 1 0 1
1 0 1 1 0 0 0 0
So our final result is 10110000 or 176. We know this is correct because we
can check our answer: 59 + 117 = 176.
Task
Add together the following binary values, show your workings:
1. 01111001 + 00011011
1. 01010101 + 00111101
2. 11101110 + 00011111
Exam Tip!
In an exam it can be tempting just to work out the answer
in denary and then write down the binary equivalent of this
number. Avoid this temptation, as questions of this type
may require you to show your working.
Binary Subtraction
0 1 1 1 1 0 1 1
Binary subtraction is the same as binary addition except
that you convert the number to be subtracted into
negative binary (using two’s complement) before adding
them together.
0 1 1 1 1 0 1 1
For example, if you want to calculate 123 − 77 in binary,
the first thing you do is write down the binary for 123. 1 0 1 1 0 0 1 1
Then you work out what −77 is in binary using two’s
complement and write this beneath it (77 is 01001101,
thus -77 is 10110011)
Finally, you add them together using the same operations 1 1 1 1 1
as for binary addition. 0 1 1 1 1 0 1 1
(Notice that the final 1 that would be carried off
the end of the final (8th) bit is just ignored.) 1 0 1 1 0 0 1 1
So the answer is 00101110 = 46 and we know this is 0 0 1 0 1 1 1 0
correct because 123 − 77 = 46.
Task
Subtract the following binary values, show your workings:
1. 11110001 – 00110011
1. 75 - 37
1. 103 – 42
Representing positive integers in hexadecimal and
converting between binary, hexadecimal and denary
Binary can be difficult for people to understand and it’s easy to make
mistakes when recording numbers. To get around this problem we often use
a base 16 number system, called hexadecimal, to represent numbers in
computing. Hex is used in computing…
• to represent colours using the RGB colour model (#FF00A1)
• within Mac addressing (00:0a:95:9d:68:16)
• to express error codes (0x32DD – see https://msdn.microsoft.com/en-
us/library/ms681381(VS.85).aspx)
You’ll need to know how to convert between denary, binary and
hexadecimal. Luckily it isn’t very hard.
Hexadecimal uses 16 symbols, the numbers 0–9 and letters A–F.
Denary Binary (4-bit) Hexadecimal
0 0000 0 9 1001 9
1 0001 1 10 1010 A
2 0010 2 11 1011 B
3 0011 3 12 1100 C
4 0100 4 13 1101 D
5 0101 5 14 1110 E
6 0110 6 15 1111 F
7 0111 7
8 1000 8
Converting Binary/Denary to Hexadecimal
To convert a denary number to hexadecimal, simply convert it to 8-bit binary. Then
split your 8-bit binary number into two 4-bit sections and convert each of these to
hexadecimal using the table above. Then split this 8-bit binary number into two 4-
bit sections:
E.g. 108 = 01101100
8 4 2 1 8 4 2 1
0 1 1 0 1 1 0 0
Converting Binary/Denary to Hexadecimal
Finally convert each of these 4-bit segments to hexadecimal:
4 + 2 = 6 (6)
8 + 4 = 12 (C)
108 in hex is 6C
8 4 2 1 8 4 2 1
0 1 1 0 1 1 0 0
Converting Hexadecimal to Denary
To convert a hexadecimal number into denary we use the column values just like what we did for
binary, but this time for a base 16 numbering system.
4096 256 16 1
163 = 4096 162 = 256 161 = 16 160 = 1
Thus, A2C as a denary number is…
4096 256 16 1
A 2 C
10 x 256 2 x 16 12 x 1
A2C is 2560 + 32 + 12 = 2604 in denary.
Task
Convert the following numbers into Hex:
1. 11110101
2. 01010101
3. 87
4. 131
Convert the following Hex values into denary:
1. A3
2. 69
3. 8AF
4. 1C2E
Floating Point Numbers in Binary
Floating point binary is used to hold really big, really small or really accurate
numbers using just 8 bits.
When dealing with very large or very small numbers, a more flexible technique,
known as floating point representation, can be employed. This allows the way
that the bits are allocated to vary so that both very large and very small numbers
can be represented
In GCSE Mathematics you learned about floating point notation (sometimes
called scientific notation). Using floating point, the number 92 can be
represented as 0.92 × 100, which is the same as 0.92 × 102.
It is this last part that we refer to as floating point notation. It is called floating
point because the number of digits is fixed, but the decimal point floats around.
• Mantissa and exponent are stored in twos complement
• 0 represents that the number is a positive number
• If the sign bit of the exponent was 1 you would be expecting a
negative number and if it was a 0 you would be expecting a positive
number.
• A positive exponent means that the binary point should be shifted to
the right and a negative exponent means that the binary point
should be shifted to the left.
In order to calculate the decimal value of the exponent calculate its value in
denary.
Put it in to a
Conversion
table to get
the denary
value.
Float the decimal 3 points to the right
Another Example
• 16 Bit register here has 10 bit for the mantissa and 6 bits for the exponent. And you can see that it has both positive.
Plug it into a
conversion
table
Example 2
Another Example with Negative Value
Floating Point Numbers in Binary
Using the example 0.92 × 102, 0.92 is the mantissa, 10 is the base and 2
is the exponent.
The mantissa is the part of the floating point number that represents the
significant digits of that number.
The exponent is the power to which the number in the mantissa is to be
raised.
Floating Point Numbers in Binary
Floating point notation allows us to store a much larger range of numbers
and store them with much more accuracy.
To show your understanding of floating point notation in binary, you will
usually be given two binary values (one for the mantissa and one for the
exponent) and asked to convert them to denary.
E.g. What is the decimal equivalent of the floating point number 01101 011
where 01101 is the mantissa and 011 is the exponent?
Shifting the binary point
• The way to convert a floating point number into denary is to use
the value of the exponent to move the binary point. The
mantissa represents a value with a binary point between the
two most significant bits. The exponent determines how far to
'float' the point to determine the final value of the number.
• If the exponent is positive, the final value will be larger than the
mantissa, so the point will 'float' to the right — the number of
places it will move is determined by the value of the exponent.
• If the exponent is negative, the point will 'float' to the left — for
the number of places determined by the exponent.
Example 1: positive mantissa, positive exponent
ACTIVITY
Example 2: positive mantissa, negative exponent
Example 3: negative mantissa, positive exponent
Example 4: negative mantissa, negative exponent
Task
Convert the following floating Convert the denary numbers into
point numbers into denary: floating point numbers (use a 6
bit mantissa and 3 bit exponent):
1. 0101001000 000100
1. +2.25
1. 0101100100 000110
1. +6.75
1. 0111000000 111111
1. +4.5
Normalisation of Floating Point Numbers
The precision of floating point binary depends on the number of bits used to represent the
mantissa. The larger number of bits used in the mantissa will allow a number to be represented
with greater accuracy, however this will reduce the number of bits in the exponent and
consequently the range of values which can be represented. A key point of note is that there will
always be a trade-off between accuracy and range when storing real numbers using floating point
notation, as there will always be a set number of bits allocated to storing real numbers with the
potential to increase or decrease the number of bits used for the mantissa against the number of
bits used for the exponent. In calculating complex qualifications sometimes the relationships
between exponent vs. mantissa can swap (2nd Year University stuff). (Known as accuracy and
range in the Specification)
For example, 67849 could be 0.67849 × 105 with 5 digits for the mantissa or 0.6785 × 104 with 4
digits for the mantissa.
In order to achieve the most accurate representation possible with the number of bits available for
a mantissa, the number should be written with no leading 0s. Normalisation is the process of
removing these leading 0s.
Normalisation of Floating Point Numbers
Mantissa Exponent
0000111 100
Step 1: Convert the exponent to denary, 100 = 4.
Step 2: Move the decimal point in the mantissa so that it is before the first 1, so
0.000111 becomes 0.111. The decimal place has moved 3 places.
Step 3: Because we’ve just moved the decimal point we need to adjust the exponent to
take this into account. As we moved to the right, we subtract the number of moves from
our exponent: 4 − 3 = 1. So our new exponent is 1.
Step 4: Our new, normalised, floating point number is 0111 001, which is the same as
0000111 100 (work it out if you don’t believe it!). But now we have loads more space in
the mantissa so we can store a much more accurate number if we wanted to.
Example 2
A binary number is presented in this format, a 5 bit mantissa and 3 bit
exponent
00111 011
Q: Is this in normalised form ?
Answer: No, consider the first two bits.
Exponent = 011 = 3
Mantissa = 0.0111, which becomes 0.111. Number of moves right is 1.
As we moved right by 1, we subtract this from the exponent to become 010.
00111 011 becomes 0111 010
Example 3
This time we are going to look at if the bit length of the Mantissa and
Exponent are provided…
E.g. A real binary number may be represented in floating point binary
notation using 5 bits for the mantissa and 3 bits for the exponent, both in
two’s complement binary. Give the normalised version of the number
00010 011
The answer would be…
01000 001 WHY?
Task
Give the normalised version of all the following numbers using 6 bits for the
mantissa and 3 bits for the exponent.
1. 000011 010
1. 001010 100
1. 000110 011
Negative Floating Point Binary Numbers
It is also possible to have a negative exponent and move the decimal
point to the left. This is achieved by storing the exponent as a two’s
complement binary number.
Mantissa Exponent 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32 1/64
01110 110 0 0 0 0 . 0 0 1 1 1 0
• Step 1: Convert the exponent to denary; in this example 110 becomes 010
or −2.
• Step 2: The mantissa started as 0.1110. Move the decimal point left 2
places to get 0.001110.
• Step 3: Write 0.001110 as denary 0.21875 (0.125 + 0.0625 + 0.03125).
Negative Floating Point Binary Numbers
The same method can be used to deal with negative numbers, representing the
mantissa using two’s complement.
Mantissa Exponent 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32 1/64
11111 010 0 . 0 1
• Step 1: Convert the exponent to denary, 010 = 2.
• Step 2: Convert the mantissa to negative binary by flipping the bits, in this
example 11111 becomes 0.0001 (think twos complement).
• Step 3: The mantissa started as 0.0001. Move the decimal point 2 places to get
0.01.
• Step 4: Write 0.01 as denary −0.25; don’t forget it’s negative!
Example 2
Mantissa Exponent
10101 011
Exponent = 011 = 3
Mantissa = 10101 = Converted to 2s comp = 0.1011
Move Mantissa 3 places right 0101.1 = -2.5
8 4 2 1 . 1/2 1/4 1/8 1/16 1/32 1/64
1 0 1 . 1
Adding Floating Point Numbers
You will be pleased to know that addition and subtraction of floating point
numbers are very similar operations. This example will walk you through adding
the two numbers below:
Mantissa (10 bit) Exponent (6 bit)
0100100000 000100
0110100000 000011
Step 1: In order to add two floating point numbers together, their exponent
must be the same. We achieve this by changing the exponent of our second
number from 000011 (3) to 000100 (4). Because we’ve changed the exponent of
the second number, we need to adjust its mantissa to take account of this, so the
mantissa becomes 0011010000 (as 0011010000 000100 is the same as
0110100000 000011).
Adding Floating Point Numbers
Step 2: Now we can add together the two mantissas using our usual
method:
0 1 0 0 1 0 0 0 0 0
0 0 1 1 0 1 0 0 0 0
0 1 1 1 1 1 0 0 0 0
Step 3: So the result is 0111110000 00100
Adding Floating Point Numbers
Lets check it back…
Mantissa (10 bit) Exponent (6 bit)
Our original data looked like this… 0100100000 000100
0110100000 000011
0100100000 000100 = 01001.00000 = 9
0110100000 000011 = 0110.100000 = 6.5
9 + 6.5 = 15.5
Answer - 0111110000 00100 = 01111.10000 = 15.5
Subtracting Floating Point Numbers
Subtraction is the same as addition except that you convert the number to
be subtracted to a negative number using two’s complement.
Mantissa (10 bit) Exponent (6 bit)
0110000000 000011
0100100000 000010
Step 1: Just like addition, the exponents of the two numbers must be the
same. We achieve this by changing the exponent of our second number
from 000010 (2) to 000011 (3). Because we’ve changed the exponent of the
second number, we need to adjust its mantissa to take account of this, so
the mantissa becomes 0010010000.
Subtracting Floating Point Numbers
Step 2: Now convert the mantissa of the second number to a negative one, using
two’s complement: 0010010000 becomes 1101110000.
Step 3: Now we can add together the two mantissas.
1 1
0 1 1 0 0 0 0 0 0 0
1 1 0 1 1 1 0 0 0 0
1 0 0 1 1 1 1 0 0 0 0
Step 4: The result is 0011110000 000011 or 3.75 (X remember to ignore any
carried values past the magnitude of the starting bit length when using 2’s comp).
Subtracting Floating Point Numbers
Lets check it back…
Mantissa (10 bit) Exponent (6 bit)
0110000000 000011
Our original data looked like this… 0100100000 000010
0110000000 000011 = 0110.000000 = 6
0100100000 000010 = 010.0100000 = 2.25
6 – 2.25 = 3.75
0011.110000 000011 = 3.75
Character Sets
As well as representing numbers, binary can also be used to represent
characters. For example, 01100001 might represent the character ‘a’. The
different representations of letters and symbols in binary are
called character sets.
The two most commonly used character sets are called ASCII and
UNICODE.
Character Sets
ASCII encodes 127 different letters and symbols using 7-bit binary codes,
which is perfect for writing in English. You won’t need to know the ASCII
value of every character but it’s worth taking the time to learn where lower
case letters (97) and uppercase letters (65) start.
http://www.ascii-code.com/
Languages like Japanese, Arabic and Hebrew all have a much larger
alphabet than English. To get around this problem, UNICODE was invented.
UNICODE uses up to four bytes to represent each character (depending on
the version). This means that it can represent up to 110 000 different
characters – more than enough to cope with most of the world’s languages.
http://unicode-table.com/en/