Fixed Point Representation of Numbers

Fixed-point Representation of
Numbers
Fixed Point Representation
of Numbers
„
„
„
„
„
„
Sign-and-magnitude representation
Two’s complement representation
Two’s complement binary arithmetic
Excess code representation
Binary fractions
Fixed point representation of fractional
numbers
Sign-and-magnitude
representation
„
magnitude of a number
„
„
the sign-and-magnitude method
„
„
„
the numerical value of the no. excluding its sign
the leftmost bit represent the sign of the no.
the remaining bits represent the binary equivalent
of the magnitude of the no.
sign bit - the bit represents the sign of a no.
Example
Express +3710 and -1410 in 8-bit binary codes
using the sign-and-magnitude method
„
„
„
convert the magnitude of the nos. into binary
3710 = 1001012
1410 = 11102
if necessary, add ‘0s’ at the left until 7 binary digits
3710 = 1001012 = 01001012
1410 = 11102 = 00011102
add a ‘0’ or ‘1’ at the left according to the sign of the no.
+3710 = 001001012
- 1410 = 100011102
Pros & Cons
of sign-and-magnitude method
„
„
Advantages
„ simple and easy to understand
Disadvantages
„ the no. zero can be represented in two ways:
00000000 and 10000000
„ the sign bit and the magnitude part have to be
handled separately complicates the design of
the circuit used for addition particularly
Two’s complement
representation
„
counters of some cassette recorders and most
video cassette recorders (0000 - 9999)
„
„
„
„
reset the counter to 0000
rewind a tape
the counter changes to 9999 & decreases gradually
similar to the design of the memory of a
computer (00000000 - 11111111)
Two’s Compl.
„
„
„
„
the leftmost bit of the binary code
„ a +ve no. Æ 0
„ a -ve no. Æ 1
the range of nos. represented is -128 to
+127
11111111+ 1 = 00000000
01111111+ 1 = 10000000
The relation between a no. & its
negative
„
„
The codes 01111110 (+126) & 100000010
(-126) are called the two’s complement of
each other
it can be obtained by
„ changing all 0s to 1s and all 1s to 0s
„ adding the resulting binary code by 1
Example
Obtain the two’s complement of 00101110 and
10100010
„
„
00101110
„ change to 11010001
„ 11010001 + 1 = 11010010
10100010
„ change to 01011101
„ 01011101 + 1 = 01011110
To convert a denary no. into an n-bit
binary code using the two’s complement
notation
„
„
„
„
convert the magnitude of the denary no. into a
binary no.
add 0s at the left until it contains n digits
if the original no. is negative, obtain the two’s
complement of the binary code
(Note that we need not find the two’s
complement if the original no. is non-negative)
Example
represent 3710 and -1410 in
8-bit two’s complement code
3710 = 1001012 = 001001012
1410 = 11102 = 000011102
„ -1410 = 111100012 + 1 = 111100102
„
To convert a 8-bit binary code back to
its denary equivalent
„
„
if the leftmost bit is 0 (i.e. the no. is +ve)
„ treat it as a simple binary no. and convert it
into denary
if the leftmost bit is 1 (i.e. the no. is -ve)
„ obtain its magnitude by finding its two’s
complement
„ change it to a denary no. and add the sign ‘-’
in front of it
Advantages of two’s complement
representation
„
„
„
„
electronic circuit used is very simple in
design
no separate circuit is needed for
subtraction
no separate circuit is needed to handle the
‘sign bit’ and the ‘magnitude part’
each no. is represented by a unique code
avoid ambiguity and widens the range of no.
represented
Two’s complement
binary arithmetic
„
„
„
„
Addition
Subtraction
Overflow
all nos. are represented in two’s complement
notation, and the no. of bits of each binary
code is fixed
Addition
„
„
„
addition is done in usual binary nos.
if a carry occurs beyond the leftmost bit,
it is discarded
Example: perform the following
additions in two’s complement binary
arithmetic using 8-bit binary codes
„
„
„
60 + 41
(-60) + (-41)
60 + (-41)
60 + 41
Check: Denary
equivalent
Carry Æ
111
00111100
+) 00101001
01100101
Ans.
01100101
60
+) 41
101
(-60) + (-41)
carryÆ
11 1
11000100
+)11010111
110011011
The extra digit is discarded
ans.
10011011
Check: Denary
equivalent
-60
+) -40
-101
60 + (-41)
Check: Denary equivalent
Carry Æ 111111
00111100
+)11010111
100010011
The extra digit is discarded
ans.
00010011
60
+) - 40
19
Subtraction
„
„
Since x - y = x + (-y),
x - y is found by adding x to the two’s
complement of y.
Example: perform the following additions in two’s
complement binary arithmetic using 8-bit binary
codes
„
„
„
60 - 41
60 - (-41)
(-60) - (-41)
= 60 + (-41)
= 60 + 41
= (-60) + 41
Overflow
„
„
„
„
overflow is said to occur when at some
stage during processing binary arithmetic, a
no. outside the finite range is generated
for 8-bit binary codes in two’s complement
form, the finite range is -12810 to 12710
concentrate on the possible occurrence of
overflow during addition only
when two nos. to be added are of the same
sign, overflow may occur
To find 86 + 60
Check: Denary equivalent
Carry Æ
11111
01010110
+)00111100
10010010
86
+) 60
146
The answer is 10010010, whose denary equivalent is -110
i.e, overflow has occurred.
Conclusion of Overflow
„
„
„
In most microcomputers, 16-bit (or
even 32-bit) binary codes are used
the range becomes -32768 to 32767
inclusively
However, no matter how many bits we
use, it is still possible to find nos.
outside the finite range
Binary fractions
„
denary nos.
1
place value
„
.
6
2
5
101 100
10-1 10-2 10-3
decimal point
binary fraction
1
place value
2
21
0
.
0
1
1
20
2-1 2-2
binary point
2-3
10.0112
= 21 + 2-2 + 2-3
= 2 + 1/4 + 1/8
= 2.37510
12.62510
= 12 + 0.625
= 12 + 5/8
= 12 + 1/2 + 1/8
= 11002 + 0.12 +
0.0012
= 1100.1012
Fixed point representation of
fractional numbers
*P. 87 - 88