1.4.1. Data Types
1.4.1. Data Types
www.pmt.education
Specification:
1.4.1 a)
● Primitive data types
○ Integer
○ Real / floating point
○ Character
○ String
○ Boolean
1.4.1 b)
● Represent positive integers in binary
1.4.1 c)
● Negative numbers in binary
○ Sign magnitude
○ Two’s complement
1.4.1 d)
● Addition and subtraction of binary integers
1.4.1 e)
● Represent positive integers in hexadecimal
1.4.1 f)
● Convert positive integers between binary, hexadecimal and
denary
1.4.1 g)
● Representation and normalisation of floating point numbers in
binary
1.4.1 h)
● How character sets are used to represent text
○ ASCII
○ Unicode
www.pmt.education
Data Types
The way in which data is represented varies between different types of data. When writing
a program, it’s essential to make sure data is being stored with the right data type, so that
the right operations can be performed on it.
Integer
An integer is a whole number. Integers include zero and negative numbers, they just can’t
have a fractional part. Integers are useful for counting things.
6 47238 -12 0 15
Real
Real numbers are positive or negative numbers which can, but do not necessarily, have a
fractional part. Reals are useful for measuring things.
R { 7 Σ ほ
String
A string is a collection of characters. While a string can be used to store a single character,
they can also be used to store many characters in succession. Strings are useful for
storing text.
www.pmt.education
Boolean
Values taken by a Boolean data type are restricted to Trueand False . Booleans are
useful for recording data that can only take two values, like the state of a power button or
whether a line of code has been executed.
True False
As explained earlier, integers are whole numbers. Computers can store whole numbers
using binary. Just like humans count in base 10, computers count in base 2, where each
step in place represents a value of two times the previous place.
A single binary digit is called a bit, and eight binary digits can be combined to form a byte.
Binary to Decimal
The least significant bit of a binary number is the one furthest to the right, while the most
significant bit is furthest to the left. When representing positive integers, the least
significant bit always represents a value of 1, with the 2nd least significant bit representing
a value of 2, then 4, 8 etc.
8 (2)
3
4 (2)
2
2 (2)
1
1 (2)
0
1 1 0 1
The diagram above shows the place value of each digit, as well as the digit’s value (either
a 0or a 1
). To work out what the number is, multiply the digit by its place value and add to
a total.
www.pmt.education
Decimal to Binary
If you have a decimal (denary) number to convert into binary, the first step is to find the
largest power of two which is smaller than the number you’re converting. Then write out
place values in powers of two up to this power.
For example, if we were converting the decimal 47 into binary, we would write out place
values up to 32. 64 is the next power of two and it’s greater than 47 so we don’t write it.
32
(2)
5
16
(2)
4
8 (
2)
3
4 (
2)
2
2 (
2)
1
1 (
2)
0
Now we need to place a 1 or a 0in each position so that the total adds up to 47. Starting
from the most significant bit (left hand side) we write a 1 if the place value is less than or
equal to our value and a 0otherwise. If we write a 1, then we subtract the place value from
our value and use the result for the next stage.
For example: the most significant bit has a value of 32, which is less than 47. Therefore,
we write a 1 under 32 and subtract 32 from 47 giving us a new value of 15.
32 16 8 4 2 1
1
We now look at the next most significant bit, and follow the same steps as before. This
time the bit represents 16 and our value is 15. The bit value is greater than our value and
so we place a 0.
32 16 8 4 2 1
1 0
Next up is 8, smaller than 15. Therefore we place a 1 and our new value is 15-8 = 7
.
32 16 8 4 2 1
1 0 1
www.pmt.education
The next most significant bit is 4. 4 is smaller than 7 and so we place a 1. Our new value is
7-4 = 3 .
32 16 8 4 2 1
1 0 1 1
Next up is 2, again smaller than our value of 3. We place a one and our new value is 1.
32 16 8 4 2 1
1 0 1 1 1
Finally, the bit represents 1 and our value is 1, so we place a 1.
32 16 8 4 2 1
1 0 1 1 1 1
We now have that the binary 101111represents the decimal number 47. You can check
your conversion using the method explained earlier for converting from binary to decimal.
It’s not unusual to see binary numbers represented as a whole number of bytes (a multiple
of eight bits) by adding leading zeros. This does not affect the value of the number. To be
represented as a byte, 47 would be written as 00101111 , with two leading 0
s.
Binary Addition
When adding binary, there are four simple rules to remember:
1. 0 +0 + 0 = 0
2. 0 +
0 + 1 = 1
3. 0 +
1 + 1 = 10
4. 1 + 1 + 1 = 11
www.pmt.education
Example: Add the binary numbers 1011and 1110
.
Place the two binary numbers above each other so that the
1 0 1 1
digits line up.
+ 1 1 1 0
Starting from the least significant bits (the right hand side),
1 0 1 1
add the values in each column and place the total below.
+ 1 1 1 0 For the first column (highlighted), rule 2 from above
applies.
1
Finally, the result is read off from the full size numbers at
1 1 0 0 1 the bottom of each column. In this case, 1011 + 1110 =
11001 .
After carrying out binary addition, it’s a good idea to check your answer by converting to
decimal if you have time.
www.pmt.education
Negative Numbers in Binary
So far we’ve covered conversion between decimal and binary for positive integers.
However, binary can represent negative numbers using a few different methods. These
methods set out rules for how a bit string should be treated, giving a special meaning to
certain bits which allows for the representation of negative numbers.
Sign Magnitude
The most basic way to represent negative numbers in binary is called sign magnitude
representation. This is the equivalent of adding a + or - sign in front of a number. However,
binary can only use 0
s and 1s, so we have to somehow represent + and - using 0and 1 .
The convention used is that a leading 1is added for a negative number, and a leading 0is
added for a positive number.
For example, the binary 1101represents the decimal number 13 . Converting to sign
magnitude means adding a 0 , or to represent -13
, add a leading 1
.
1101 0
1101 1
1101
Converting from sign magnitude to decimal is as simple as making a note of the most
significant bit, remembering the sign and discarding the leading bit. Then convert the
remaining bits to decimal using the method explained earlier and add the sign.
For example, the sign magnitude number 10110is negative, because it starts with a 1.
Remove the 1and we’re left with 0110which is 6(4+2)in decimal. Add on the minus
sign and we have our result: -6
.
Two’s Complement
Another method of representing negative numbers in binary, two’s complement has the
added advantage of making binary arithmetic with negative numbers much more simple.
Two’s complement works by making the most significant bit negative. For example, with
four bits (half a byte) the most significant bit, usually 8, represents -8.
Converting to two’s complement is as simple as flipping all of the bits in the positive
version of a binary number and adding one. For example, the binary half-byte representing
7 is 0111
. Flip all the bits and you get 1000
, adding one gives us 1001
.
www.pmt.education
Writing in the bit values, we can see how this works.
-8 4 2 1
1 0 0 1
Calculating the equivalent in decimal, using the method explained earlier, we have:
(-8×1)+(4×0)+(2×0)+(1×1) = -
7
Two’s complement makes subtraction in binary easy. Subtracting a number from another
is the same as adding a negative number. This is how binary subtraction works.
www.pmt.education
Hexadecimal
In the same way that decimal is base 10, and binary is base 2, hexadecimal is base 16. In
addition to the numbers 0-9
, hexadecimal makes use of the characters A-Fto represent
10-15 .
Decimal
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 A B C D E F
Hexadecimal
Just like binary, place values in hexadecimal start with 1 (160) and go up in powers of 16.
256 (16)
2
16 (16)
1
1 (16)
0
2 D 3
Remembering that E represents 14 in decimal and that F represents 15, we have:
(2×256)+(13×16)+(3×1) = 723
www.pmt.education
Converting from hexadecimal to binary
To convert hexadecimal to binary, first convert each hexadecimal digit to a decimal digit
and then to a binary nybble (four bits, half a byte) before combining the nybbles to form a
single binary number.
B2
Split into hexadecimal digits
B 2
Convert hexadecimal to decimal
11 2
Convert decimal to binary nybbles
1011 0010
Combine binary nybbles
10110010
128 64 32 16 8 4 2 1
1 0 1 1 0 0 1 0
128 + 32 + 16 + 2 = 178
B2= 178
www.pmt.education
Converting from hexadecimal to decimal
One way to convert from hexadecimal to decimal is to first convert to binary, as explained
above, and then convert from binary to decimal. Alternatively, use the place values of
hexadecimal to convert directly to decimal.
256 (16)
2
16 (16)
1
1 (16)
0
4 C 3
For example, 4C3 in hexadecimal is (4
×256
)+(12
×16
)+(3
×1
) = 1219
You can think of floating point binary as being like scientific notation. Take the following
example:
Floating point numbers can be split into two parts: mantissa and exponent. In this case,
the mantissa is 6.67 and the exponent is -11. When combined, the mantissa and exponent
provide all the information needed to work out the actual value being represented.
In this case, the scientific notation represents the value 0.0000000000667. The value 6.67
is shifted 11 times from the decimal point.
We can do the same in binary, provided that we include information about the size of the
mantissa and exponent. We also dedicate a single bit to the sign - whether a number is
positive or negative.
Take for example a structure with a leading sign bit, an 8-bit mantissa and a 4-bit two’s
complement exponent.
S M E
0 1 1 0 0 1 0 0 1 0 1 0 1
The mantissa is always taken to have the binary point (the equivalent of a decimal point,
but in binary) after the most significant bit. So this mantissa is actually 1.1001001
.
www.pmt.education
Next we convert the exponent to decimal using the method explained earlier. In this case,
the exponent is 5.
Combining the three parts, we need to move the binary point five places to the right, giving
us 110010.01. We can then convert this to decimal as follows:
0.5 0.25
32 16 8 4 2 1
(2 )
-1
(2 )
-2
1 1 0 0 1 0 • 0 1
32 + 16 + 2 + 0.25 =
50.25
Second example
As before, we’re using a format with a single sign bit, an 8-bit mantissa and a 4-bit two’s
complement exponent.
S M E
1 0 1 0 1 1 0 0 0 1 1 0 1
The sign is 1 and so we’re dealing with a negative number. The mantissa is 0.101101000
and the exponent is (remembering it’s in two’s complement) -8 + 4 + 1 = -3.
We move the binary point from between the two most significant bits of the mantissa three
places to the left, giving us 0.0001011.
1 1
2
1
4
1
8
1
16
1
32
1
64
1
128
0 • 0 0 0 1 0 1 1
11
Which is 128
in base 10.
www.pmt.education
Normalisation
Floating point numbers are normalised in order to provide the maximum level of precision
for a given number of bits. Normalisation involves ensuring that floating point numbers
start with 01(for a positive number) or 10(for negative numbers).
0 0 1 0 0 1 0 1
Mantissa Exponent
Because we’ve made the mantissa bigger by shifting the bits one position to the left, we
must reduce the exponent by one so as to ensure the same number is still represented.
The current exponent is 510 so, subtracting one, the new exponent must be 410 which is
01002 in binary.
0 1 0 0 0 1 0 0
Mantissa Exponent
www.pmt.education
Character Sets for Representing Text
A character set is a published collection of codes and corresponding characters which can
be used by computers for representing text.
ASCII
Standing for American Standard Code for Information Interchange, ASCII was the leading
character set before Unicode. ASCII uses 7 bits to represent 27 = 128 different characters.
The capital letters A-Z are represented by codes 65-90 while the lower case letters a-z
correspond to codes 97-122. There are also codes for numbers and symbols.
While 128 characters is plenty for standard letters, numbers and symbols, ASCII soon
came into trouble when computers needed to represent other languages with different
characters.
Unicode
Unicode solves the problem of ASCII’s limited character set. Unicode uses a varying
number of bits allowing for over 1 million different characters, many of which have yet to
be allocated. Because of this, Unicode has enough capacity to represent a wealth of
different languages, symbols and emoji.
www.pmt.education