13 Data representation
This chapter considers further techniques that are used to represent data in the
computer. It builds up from what was done under the AS syllabus.
Two’s complement
The two’s complement notation is used to represent signed numbers in binary. The
concept was done at advanced subsidiary level. This chapter retakes the concept as it has
implications in floating point representation.
Converting a denary number to two’s complement.
Steps
Ignore the sign and convert the number into binary.
Add leading zeroes to fit the size of the register.
If the number is positive, regard this as the answer otherwise proceed with the
following steps.
Flip ones to zeroes and zeroes to ones.
Add 1 to get the result.
Positive two’s complement numbers start with a 0 while negative ones start with a 1
Example: Converting 115 to two’s complement using 8 bits.
Fitting into an 8-bit register
Answer: 01110011
= 1110011
Example. Converting - 115 to two’s complement using 8 bits.
115 in binary is 1110011
0 1 1 1 0 0 1 1
Flipping 1 0 0 0 1 1 0 0
Add 1 + 1
Result 1 0 0 0 1 1 0 1
Answer: 10001101
Place values of two’s complement numbers
The first bit of a two’s complement number is -128. The bits that follow are decreasing
powers of 2. The following example illustrates this using the 8-bit number in the example
above. – 115 = 10001101, so the place values are as follows.
Place value -128 64 32 16 8 4 2 1
Digit on that place 1 0 0 0 1 1 0 1
Using place values to convert a negative denary number into two’s complement.
A number can be converted into two’s complement using the concept of place values
discussed above.
5 PASIPANODYA H
Steps
Express the denary number as a sum of -128 and a number, 𝑎
Insert 1 under the -128 place.
Convert 𝑎 to binary.
Insert the binary pattern of 𝑎 into the remaining bits starting from the left.
Example: Converting -115 to binary.
-115 =- 128 +13
Place value -128 64 32 16 8 4 2 1
Digit on that place 1
Denary 13 = Binary 1101
Place value -128 64 32 16 8 4 2 1
Digit on that place 1 0 0 0 1 1 0 1
Answer: 10001101
Converting two’s complement numbers to denary
The following are two methods that one can use to convert a number that is given in two’s
complement into denary.
Example: Converting 10001101 using place values
Since the number has 8 bits, the following are the place values for the number.
Place value -128 64 32 16 8 4 2 1
Digit on that place 1 0 0 0 1 1 0 1
Considering 1-bits we have
128 + 8 + 4+1 = -115
Answer: -115
Example: Using Converting 10001101 reverse steps.
The steps used to convert a number to two’s complement can be used in reverse when
one needs to convert a two’s complement number to denary. The reverse steps will now
be as follows:
Steps
Check if the number starts with a 1 or a 0.
If the number starts with a zero, it positive. Just convert it to denary to get the
answer.
If the number starts with a 1. It is negative. Do the following steps.
Add 1 to the number
Flip ones to zeros and zeros to ones.
Convert the resultant number to denary.
Insert a negative sign.
1 0 0 0 1 1 0 1
Subtract 1 1
Result 1 0 0 0 1 1 0 0
Flip 0 1 1 1 0 0 1 1
Convert 0 111 00 11 to denary. This gives 115
6 PASIPANODYA H
Answer – 115
Binary fractions
As learnt under AS level, positions within a binary number are successive powers of 2.
This concept extends to binary fractions. Places before the coma are positive powers of 2
while places after the coma are negative powers of 2.
Example: converting 10.111 to denary.
10.111= 1×2 +0×2 +1×2 +1×2 +1×2
= 1×2+0×2+1× +1× +1×
Answer = 2.875
To convert a decimal fraction to a binary fraction one must follow the following steps.
Steps
Multiply the decimal by 2 and take note of the result.
Take down the integer part of the result.
Take the fractional part and repeat the steps.
Repeat this until the fractional part is 0, otherwise continue the process up to the
required level of significance.
Read the answer from the top.
Example: Converting 0.625 to binary
Multiplication by 2 Result Integer part
0.625 × 2 1.25 1
0.25 × 2 0.5 0
0.5 × 2 1.0 1
Answer: 101
Example: Converting 0.2 to binary
Multiplication by 2 Result Integer part
0.2 × 2 0.4 0
0.4 × 2 0.8 0
0.8 × 2 1.6 1
0.6 × 2 1.2 1
0.2 × 2 0.4 0
0.4 × 2 0.8 0
0.8 × 2 1.6 1
0.6 × 2 1.2 1
The result cannot be written in an exact manner due to recurrence, so there is need to
round off.
Answer: Decimal 0.2 = binary 0.0011 to 4 binary places
Floating-point representation
Real numbers can be represented as floating-point numbers. They are so-called because
of the coma which can float from one point to the other without changing the value of the
number. This is illustrated below using a denary number.
20348×10 =2034.8×10 =203.48×10 =20.348×10
The Floating-point numbers are written in the form M × BE where M is the mantissa, B
is the number base and E is the exponent.
7 PASIPANODYA H
Binary floating-point numbers are written in the form
M × 2E
If the first bit of a floating-point number is 1, the number is negative. If the bit is 0, then
the number is positive.
Steps to covert a denary number to floating-point form.
Convert the number to binary.
Express the number in index form, that is M × 2 E
Normalise the number, that is moving the comma until the number starts with
0.1….
Add trailing zeros on the mantissa where necessary.
Add leading zeros on the exponent where necessary.
Store the mantissa and exponent in two’s complement. The base is not stored since
it is presumed to be 2.
Floating-point numbers with positive mantissa and exponent.
Example: Converting 45.125 to floating-point representation using 12 bits for mantissa
and 4 bits for exponent.
45 is 101101
Converting 0.125 binary.
Multiplication by 2 Result Integer part
0.125 × 2 0.25 0
0.25 × 2 0.5 0
0.5 × 2 1.0 1
0.125= 001 in binary
45.125= 101101.001
=00101101.001 × 2
=0.101101001 × 2
Mantissa = 0.10110100100 Exponent = 0110
Answer: 010110100100 0110
Floating-point numbers with negative mantissa
Example: Converting -20.5 to floating-point representation using 8 bits for mantissa and
8 bits for exponent.
-20.5
Converting -20.5 binary.
Multiplication by 2 Result Integer part
0.5 × 2 1.0 1
0.5= 1 in binary
-20.5= -10100.1
=-10100.1 × 2
=-0.101001× 2
Mantissa = -0.1010010 Exponent = 00000101
The mantissa is negative, so its two’s complement representation is as follows.
8 PASIPANODYA H
0 1 0 1 0 0 1 0
Flip 1 0 1 0 1 1 0 1
Add 1
Flip 1 0 1 0 1 1 1 0
Answer: 10101110 00000101
Floating-point numbers with negative exponent
Example: Converting 0.375 to floating-point representation using 8 bits for mantissa and
8 bits for exponent.
Converting 0.375 binary.
Multiplication by 2 Result Integer part
0.375 × 2 0.75 0
0.75 × 2 1.5 1
0.5 × 2 1.0 1
0.375= 0.011 in binary
=0.011 × 2
=0.1100000× 2
Mantissa = 01100000 Exponent = - 000000001
The mantissa is negative, so its two’s complement representation is as follows.
0 0 0 0 0 0 0 1
Flip 1 1 1 1 1 1 1 0
Add 1
Flip 1 1 1 1 1 1 1 1
Answer: 01100000 11111111
Normalisation of floating-point numbers
The process of moving the comma until it is in a standard position is called normalisation.
Normalised floating-point numbers should meet the following conditions.
The first and second bits should be different.
The comma should lie between the first and the second digit.
The following are the reasons for normalising floating point numbers.
To maximise the precision of the numbers that can be stored.
To make sure that there is only one representation of a number.
Example: Normalising a positive floating-point number stored in a computer as
00001101 00000001
From the arrangement one can see that there are 8 bits for the mantissa and 8bits for the
exponent. The computer assumes the comma to lie between the first and the second bits,
so we have:
00001101 00000001= 0.0001101×21
=0.1101×21-3
= 0.1101×2-2
Using 8 bits, -2 =11111110
Answers: 01101000 11111110
Example: Normalising the negative number 1110101100 000101
From the arrangement one can see that there are 10 bits for the mantissa and 6 bits for
the exponent. The computer assumes the comma to lie between the first and the second
bits, so we have:
9 PASIPANODYA H
00001101 00000001= 1.110101100×25
=111.0101100×25-2
= 1.010110000×23
Using 6 bits, 3 =000011
Answers: 1.010110000 000011
Place values for floating point numbers.
The Most significant bit (MSB)of the mantissa of a normalised floating-point number has
a place value of -1. If this bit is a 1, t hen the number will be negative. If the MSB is 0, then
the floating-point number will be positive. Bits that follow the MSB in the mantissa are
decreasing powers of 2 starting at 2-1.
The place values for the exponent are the same as those used in twos’ complement
representation.
8 bits for the mantissa and 8 bits for the exponent.
-1 1 1 1 1 1 1 1 -128 64 32 16 8 4 2 1
2 4 8 16 32 64 128
5 bits for the mantissa and 3 bits for the exponent.
-1 1 1 1 1 -4 2 1
2 4 8 16
Converting floating-point number to denary
Example: Converting the floating-point number 10100011 00000011 to denary.
The place values for the number are as follows.
-1 1 1 1 1 1 1 1 -128 64 32 16 8 4 2 1
2 4 8 16 32 64 128
1 0 1 0 0 0 1 1 0 0 0 0 0 0 1 1
MANTISSA EXPONENT
Expressing the number in index form, we have
(-1+ + + )× 23 = (-1 + ) × 23 =− ×8
Answer: -5.8125
Example: Converting the floating-point number 010100000000 11 10 to denary.
Note that 1110 is the twos’ complement value for -2
So, 010100000000 11 10= + ×2-2
= ×
Answer: 0.15625
Representing zero in floating point.
In a normalised floating-point number, the first and the second digits must be different.
This condition makes it impossible to represent zero as a normalised floating-point
number.
Range and accuracy of floating-point numbers.
Consider that there is need to store the floating-point number 011011010111 0101 in
floating point form. Using 12 bits for the mantissa and 4 bits for exponent we have the
following scenario.
10 PASIPANODYA H
0 1 1 0 1 1 0 1 0 1 1 1 0 1 0 1
If 8 bits are then used for both the mantissa and the exponent, the set up will be as follows:
0 1 1 0 1 1 0 1 0 0 0 0 0 1 0 1
It can be observed that reducing the number of bits in the mantissa will result in some
right-most bits being lost. This results in loss of precision.
On the other hand, the value stored in the exponent determines the number of place
values that the floating-point number has. Thus, adjusting the size of the exponent
influences the range of the floating-point number.
Points to note.
Increasing the bits for the mantissa increases precision while reducing the bits
results in loss of precision.
Increasing the bits for the exponent increases the range of the number while
reducing the bits reduces the range of the number.
The total number of bits for the exponent and mantissa is fixed. This means that
adjusting the number of bits is a trade-off between accuracy and range as it is not
possible to increase the bits of the mantissa without reducing the bits of the
exponent and vice versa.
Highest and lowest magnitude floating-point numbers.
Magnitude refers of how mush a number of deviates from zero. For positive numbers, the
larger the number the bigger the magnitude. For positive numbers, the smaller the
number the bigger the magnitude.
Positive numbers
The largest magnitude positive number should have the largest possible mantissa and the
largest possible exponent.
0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
The smallest magnitude positive number should have the smallest possible mantissa and
the smallest exponent.
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0
Negative numbers
The largest magnitude negative number should have the smallest possible mantissa and
the largest possible exponent.
1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
The smallest magnitude negative number should have the highest possible mantissa and
the smallest possible exponent.
1 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0
Arithmetic errors
Arithmetic errors associated with floating point numbers are overflow and underflow
errors.
11 PASIPANODYA H
For twos’ complement the highest number that can be stored in 8 bits is:
0 1 1 1 1 1 1 1
=+127
The lowest number is:
1 0 0 0 0 0 0 0
= - 128
Any number that falls outside the range -128 to + 127 will result in an arithmetic error.
Overflow occurs when the number to be represented is too large for the allocated bits.
Example:
(a) Representing 300 in twos’ complement gives 100101100 which exceeds the
number of bits.
(b) Representing 150 in twos’ complement gives 10010110. The number is positive
but looks like a negative number.
Underflow occurs when the number to be represented is too small to be represented
within the allocated bits.
Example: Representing -130 gives 01111110. The number looks like a positive number
when its negative.
12 PASIPANODYA H