Uploaded by tanakamuteta925

1. Data representation

advertisement
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051
SECTION 1: DATA REPRESENTATION
Data Representation
Fixed Point integer representation
There is no memory space for the decimal point. However computers represent a finite
number of digits. This limitation allows us to evaluate the maximum and minimum possible
numbers that can be represented. These include:




Maximum Positive Number:Positive numbers start with a 0, thus 01111111 = +127
Minimum Positive Number:This evaluates to 00000001 = +1
Smallest Magnitude Negative Number:Negative numbers start with a 1. This gives us 11111111 = - 1
Largest Magnitude Negative Number:This is 10000000 = -128
Fixed point binary number system
The fixed point number representation assumes that the binary point is fixed at one
position. The binary point is not actually present in the register, but its presence is assumed
based on whether the number which is stored is a fraction or an integer. Thus fixed point
numbers can either be fractional or integer.
Example: Convert denary number 6.1875 to binary
2
2
2
6
3
1
0
.1875
R 0
R 1
R 1
= 110
.1875 x 2 =
0.375
0
.375 x 2 =
0.75
0
.75 x 2 =
1.5
1
.5 x 2 =
1.0
1
6.1875 = 0110.0011
Example: Convert the floating point binary number 0110.1100 to denary
0
1
1
0
8
4
2
1
0
4
2
0
4 +2+
0
0
0
0
1
1
+
1|Page
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051
= 6.1875
0110.0011 = 6.1875
Example: Convert denary number -3.1875 to binary
2
2
2
.1875
3
1
0
R 1
R 1
= 11
.1875 x 2 =
0.375
0
.375 x 2 =
0.75
0
.75 x 2 =
1.5
1
.5 x 2 =
1.0
1
3.1875 = 0011.0011
•
0
1
=
•
1
+
1
Change all the 0s to 1s and all the 1s to 0s
0
1
1
0
1
0
0
1
0
1
1
0
1
0
Add 1 to the result.
1
0
0
1
1
0
1
0
0
1
1
0
0
1
1
– 3.1875 = 1100.1101
Example: Convert a signed floating point binary number 1100.1101 to denary
1
1
0
0
-8
4
2
1
0
4
2
0
-8 + 4 +
1
1
0
0
0
1
+
1100.1101 = – 3.1875
Advantages and Disadvantages of the Fixed Point Binary System
The advantage of fixed point notation is simple arithmetic (same as integer arithmetic) and
therefore faster processing. However, a disadvantage is the limited range, as increasing the
number of bits after the binary point for precision decreases the range and vice versa
Floating point binary
2|Page
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051
Fixed point representation allows the computer to hold fractions, but the range of numbers is still
limited. Even using 4 bytes (32 bits) to hold each number, with 8 bits for the fractional part after the
point, the largest number that can be held is just over 8 million.
Another format is needed for holding very large numbers
In decimal, we can show very large numbers in scientific notation. For example
1 200 000 000 000
can be written as
0.12 x
Here 0.12 is called the mantissa (or coefficient) and 13 is called the exponent. The mantissa
holds the digits and the exponent defines where to place the decimal point. In example
above the point is moved 13 places to the right
The same technique can be used for binary numbers. For example, two bytes (16 bits) might
be divided into 10 bits for the mantissa and 6 bits for the exponent.
mantissa
0
110100000
exponent
000011
=
0.1101 x
Sign bit
The sign bit (0) tells us that the number is positive. The mantissa represents 0.1101 and the
exponent tells us to move the point 3 places right, so the number becomes 110.1 which
converted to decimal is 6.5
Note: that the point starts off between the sign bit and the first bit of the mantissa.
The rules for converting binary floating point number to decimal




Place the point between the sign bit and the first digit of the mantissa.
Convert the exponent to its equivalent decimal form (positive or negative)
Move the point right if the exponent is positive, or left if the exponent is negative,
the appropriate number of places.
Convert the resulting binary number to denary.
Example: convert the following floating point binary number to decimal with 10 bit mantissa
and 8 bit exponent:
0 100000000 11111110
 0.100000000 11111110
 0.1
exponent – 2
 0.001
3|Page
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051

0
0
0
0
0
1
1
0
= 0.125
Exercise:
Converting a negative binary floating point number to decimal is similar. However you need
to remember that after moving the binary point, with a positive exponent the left-most bit
will have a negative value
Also if the exponent is negative (indicated by a 1 in its leftmost bit) the binary point is
moved left instead of right. You need to fill in with extra 1’s as you shift the binary point to
the left.
Example: convert the following floating point binary number to decimal with 10 bit mantissa
and 8 bit exponent:
1 100000000 11111110
 1.100000000 11111110
 1.1
exponent – 2
 1.111
1
1
1
1
-1
-1
-1 +
+
= - 0.125
Exercise:
4|Page
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051
The rules for normalisation of binary floating point number





Place the point between the sign bit and the first digit of the mantissa.
Convert the exponent to its equivalent decimal form (positive or negative)
Shift the point to the right or to the left to achieve a normalised state, add the
number of places moved to the left to the exponent or subtract if the point was
moved to the right.
Convert the back the exponent to binary maintain the number of bits.
Maintain number of bits of the mantissa by adding 0s to the right.
Example: Normalise the floating point binary number: 0 000110101
000010
0 000110101 000010
 0.000110101 exponent 2
 0.110101
exponent 2 - 3
 0.110101
exponent - 1
0 110101000 111111
Normalised
Example: Normalise the floating point binary number: 1 111100100
1 111100100
 1.111100100
 1.00100
 1.00100
1.001000000
000011
000011
exponent 3
exponent 3 - 4
exponent - 1
111111
Normalised
5|Page
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051
Exercise:
Normalisation of decimal numbers
Example: Normalise the decimal number 3.1875 using 8 bit for the mantissa and 4 bit for
the exponent.
2
2
2
3
1
0
.1875
R 1
R 1
.1875 x 2 =
0.375
0
.375 x 2 =
0.75
0
.75 x 2 =
1.5
1
.5 x 2 =
1.0
1
= 11
3.1875 = 0011.0011
 0011.0011
exponent 0
 0.110011
exponent 0 + 2
 0.110011
exponent 2
0 1100110
0010
Normalised
6|Page
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051
Example: Normalise the decimal number -3.1875 using 8 bit for the mantissa and 4 bit for
the exponent.
2
2
2
3
1
0
.1875
R 1
R 1
.1875 x 2 =
0.375
0
.375 x 2 =
0.75
0
.75 x 2 =
1.5
1
.5 x 2 =
1.0
1
= 11
3.1875 = 0011.0011
•
Change all the 0s to 1s and all the 1s to 0s
0
1
=
•
1
+
1
0
1
1
0
1
0
0
1
0
1
1
0
1
0
Add 1 to the result.
1
0
0
1
1
0
1
0
0
1
1
0
 1100.1101
 1.001101
 1.001101
1 0011010
0
1
1
exponent 0
exponent 0 + 2
exponent 2
0010
Normalised
Advantages of normalised numbers






maximum precision for a given number of bits
only one representation for each number
Ensures that a single representation of a number is maintained (standardisation).
Ensures maximum possible accuracy with a given number of bits is maintained.
Can be used to detect error conditions such as underflow and overflow
Tires to maximise the range of numbers that can be represented in a fixed point
representation (Range and accuracy is limited in fixed point representation)
Range and Precision/ Accuracy and Range

The size of the exponent determines the range of numbers that can be
represented
7|Page
Alevel ZIMSEC Computer Science Notes


Compiled by Engineer Nyamz
+265994961051
 The range of numbers is expanded by increasing the number of bits that
are used to represent the exponent. This will however decrease
precision.
 Reducing the number of bits in the exponent will reduce the range
because power of two which the mantissa is multiplying by is decreased.
 At the same time decreasing the exponent’s bits will increase accuracy
because more digits are represented after the binary point.
The size of the significant determines the precision of the numbers that can be
represented
 Precision can be increased by increasing the number of bits that are used
to represent the significant
 This will decrease the range.
The only way to increase both range and precision is to use more bits
 that is, the use of single-precision numbers, double-precision numbers,
8|Page
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051
Computer arithmetic errors
What are Overflow and underflow
Overflow
Occurs when calculations produce results exceeding the capacity of the result.
Example:
16 bit integers can hold numbers in the range -32768 …32767. So what happens when you
add 2000 to 2000
The siteenth bit contains a ’1’ as a result of adding the two numbers. Yet, numbers with a
’1’ in the leading position are interpreted as negative numbers, so instead of ‘4000’ the
result is interpreted as ‘25536’.
Overflow can also occur in the exponent of a floating point number, when the exponent has
become too large to be represented using the given representation for floating-point
numbers (for example 7 bits for 32-bit integers, or exponents larger than 63).
Underflow
A calculation resulting in a number so small that the negative number used for the exponent
is beyond the number of bits used for exponents is called underflow (example 7 bits for 32bit integers, or exponents smaller than -64)
The term arithmetic underflow ( or “floating point underflow”, or just “underflow”) is a
condition in a computer program where the resultof a calculation is a number of smaller
absolute value than the computer can actually store in memory.
9|Page
Alevel ZIMSEC Computer Science Notes
Compiled by Engineer Nyamz
+265994961051
Overflow
A CPU with a capacity of 8bits has a capacity of up to 11111111 in binary. If one more bit
was added there would be an overflow error
Example: 8-bit overflow
An example of an 8-bit overflow occurs in the binary sum 11111111 + 1 (denary 255 + 1)
The total is a number bigger than 8 digits, and when this happens the CPU drops overflow
digit because the computer cannot store it anywhere, and the computer thinks 255 + 1 = 0
10 | P a g e
Download