notes 2-chapt 2

advertisement
Summary- Representing Integers and Real NumbersPoitive and Negative- With Notes On Addition
INTEGERS
n+1 bit Binary Representations [n,n-1,......, 1 0] , bit n is the Sign bit, 0 to bit n-1 represent Magnitude.
Sign Magnitude:
May Need a combination of 2 of the following for Addition of positive and Negative Numbers
Adder, Subtracter , 2's Complementer,
A) For Conversion
Positive to Negative Numbers or visa-versa. Change Sign Bit
B) For Adding
I Two Positive Numbers-Add Magnitudes Sign Bit = 0
II Two Negative Numbers-Add Magnitudes Sign Bit =1
Overflow in I or II: If 1 in bit n after Addition
III One Positive Number and One Negative Number:
1) a) Find Largest b) Subtract Magnitude of Smallest from Largest Sign Bit = Sign of largest.
2) Convert Negative Number to 2's Complement and Add-Convert result back to Sign Magnitude
if negative.
Overflow can’t occur
2's Complement:
Need Adder and 2's Complementer
A) For Conversion
Positive to Negative Numbers or vica-versa. Use 2's Complementer
B) For Adding
Once Both Numbers are in 2's Complement Add (Add entire representation including bit n.)
Overflow: Two Positive Numbers- 1 in Sign Bit,
Two Negative Numbers- 0 in Sign Bit
One Positive Number and One Negative Number: Overflow can’t occur
Real Numbers
Real represented as a Normalized Binary Number in Sign Magnitude x a power of 2which indicates how far and
in which direction the binary point is to be moved,
Adding such numbers requires equalizing the exponents and shifting the exponent accordingly adding the
reulting mantissas and re-normalizing the result.
© M C Paull 2008
1
I Positional Notation For Positive Integers
Example
4 Bit Binary:
Represents
2digit Decimal: Represents
4 3 2 1 0 (Positions)
1 1 0 1 = 1x 23 + 1x 22 + 0x 21 + 1x 20 = 13 =
1 1 1 1 = 1x 23 + 1 x 22 + 1x 21 + 1x20 = 15 =
0 0 0 0 = 0x23 + 0 x 22 + 0x 21 + 0x20 = 0 =
(1x10 1 + 3x100 )
(1x10 1 + 5x100 ) Highest
( 0x10 1 + 0x100 ) Lowest
II a) Positional Notation For Signed Integers- Sign Magnitude
1 Sign Bit in High order position 0 = +, 1 = 5 Bit Binary For Magnitude :
Represents
2digit Decimal
4 3 2 1 0 (Positions)
+ 1x 23 + 1x 22 + 0x 21 + 1x 20 = + 11
[ 0 | 1 0 1 1] =
- [ 1x 23 + 1x 22 + 0x 21 + 1x 20 ] = - 11
[ 1 | 1 0 1 1] =
[ 0 | 1 1 1 1] =
[ 1 |1 1 1 1] =
+ 1x 23 + 1 x 22 + 1x 21 + 1x20 = + 15 Highest
- [ 1x 23 + 1 x 22 + 1x 21 + 1x20 ] = -15 Lowest
10000?
b) Positional Notation For Signed Integers- 2‘s Complement
The 1 Sign Bit is in the High order position. If it = 0 then the number is positive +, if 1 it is negative 5 Bit Binary For Magnitude :
Represents
2digit Decimal
4 3 2 1 0 (Positions)
[0 |1 1 0 1 =
+ 1x 23 + 1x 22 + 0x 21 + 1x 20 = +1 3
Take 2‘s complement = 1’s complement & add 1
[1 | 0 0 1 0] =
+1
[1 | 0 0 1 1] =
[1 |0 0 1 1] =
1’s complement
[0 | 1 1 1 1 ] =
+ 1x 23 + 1 x 22 + 1x 21 + 1x20 = + 15 Highest
2‘s complement
= - 13
[1| 0 0 0 0]
© M C Paull 2008
-16, Lowest
2
11111
11110 -1
-2
11101 -3
00000
00001
0
1
00010
2
00011
3
4
-4
5
-5
6
-6
11001
-7
+
00111
7
8
9
0
+
10
12
-14
Negative + Negative
-15
10001
-16 1501111
10000
Positive + Positive
14
0
Positive + Negative
0
0
nonoverflow
-16 15
0
0
-16 15
-16 15
-16 15
0
0
0
overflow
15
-16
© M C Paull 2008
15
-16
15
-16
If numbers < |16| overflow impossible
15
-16
3
Binary-1s and 0s is the language of the Computer. So all number, positive, negative, integer, reals, etc.
are represented as sequences of bits-1s and 0s (as are characters, etc.)
The computer has hardware to add, subtract, etc. binary numbers, thus represented. Addition and
Subtraction algorithm used are analogous to that used with decimal numbers.
2's COMPLEMENT
SIGN_MAGNITUDE
0
+ 0
1
0
0
0
1
1
1<--- 1<---
0
1
1
0
+ 0
1 = 11
1 = 3
1
Addition
0
0 = 14
1
1
0 = 14
1
0
0
1
1 = -3
0
1
0
1
1 = 11
1
1
1 = 11
1 = 3
carries
1
1
1
0 = 14
Addition
0--> 0 --> borrows
1
0
0
1<--- 1<---
carries
0
1
0
0
1
1
1
0 = 14
1
1
1
0
1 = -3 in 2‘s complement**
1
carries
1 = 11
1<--- 1<---
0
Subtraction = Subtract Magnitudes
1
0
Subtraction-Get 2's complement and Add
Smaller from Larger adjust sign
0--> 0 --> borrows
0
1
1
1
0 = 14
1
0
0
1
1 = -3
1
1
0
1
1 = 11
0
0
1
0 = - 14 in 2‘s complement**
0
0
0
1
1 =
1
0
1<---
Subtraction = Subtract Magnitudes
Smaller from Larger adjust sign
1
1
0
3
carries
1 = -11
Subtraction-Get 2‘s complement and Add
** 0 0 0
1 1 1
1 1
1
0
1
0 1 0
0
1
1 = 3
0 = 1's complement
+ 1
1 = 2‘s complement of 3
1 = 11 = 2's complement -11
Sign Extension SEXT
There are situations dealing with numbers which do not require the full N bits that are available in a typical
word. Say we need only M < N bits of storage to hold such numbers. However it is will often be necessary to
perform binary arithmetic operations like addition with an M bit number and an N bit number. Whatever
their length, one bit is required to indicate the sign of the number. So there are N-1 bits and M-1 bits for the
magnitude and one additional bit for the sign respectively. To add two numbers of different lengths it is necessary
to pad or extend the M bit number on its left to make its length N bits before adding it to the N bit number.
The details of that padding depends the number representation- sign-magnitude or 2‘s complement notation.
© M C Paull 2008
ADDITION AND SUBTRACTION
4
In the sign-magnitude case extend the smaller number with N-M 0’s to the left of the magnitude and make the original
sign (1=negative, 0=positive) the first bit. Then the addition or subtraction whatever is needed as determined by the signs
is carried out.
In the 2‘s complement case again the M bit number must be expanded to N bits. before the addition. One simply
extend the M bit number by extending (repeating its sign bit to the left until there are N bits. The following example has
N=8 and M = 4.
00001011= 11
0101 = 5
00110000 = 48
11110101 = -11
00001011 = 11
padding--> 00000101 = 5 padding--> 00000101 = 5
padding--> 11111011 = -5
11111010
=
-6
00000110 = 6
00110101 = 53
(00000110) = 6
Example 2's complement Sign Extension (padding)
SIGN_MAGNITUDE
0
0
1
1 0
0 1
0
0
+ 0
1 1 = 11
1 0 = 6
carries
1<--- 1<---
1
2's COMPLEMENT
0
1
0
0
0
1
0
0
1
1
1
1
0
0 0
0
1 = 11
0 = 6
carries
1 = 17
Addition
Addition
1
1
1
1
1<--- 1<---
1
1 = 14
0
1
1 = 11
0 = 6
carries
1 = 14
1
+ 1
0
1
1
0
1 0
1
0
1
1<--- 1<---
1
1
1 = - 11
0 = -6
carries
1 = 17
Addition-Magnitudes Only
Addition
OVERFLOW
Addition Sum of Magnitudes
> Highest 2n-1 or Carries
OVERFLOW
After Addition
a) Both Positive- 1 in Sign Bit
a) Both Negaitive - 0 in Sign Bit
Into Sign Bit
0--> 0 --> borrows
0
1
1
1
0 = 14
1
0
0
1
1 = -3
0
1
0
1
1 = 11
1
0
0
0
1
0
1
1<---
1
0
1
0
0 = - 14 in 2‘s complement
1 =
3
carries
1 = -11
NO OVERFLOW
NO OVERFLOW
1 Negative and 1 Positive Number
With Both Numbers In Ranges
Subtraction
With Both Numbers In Range
OVERFLOW
© M C Paull 2006
0
5
Binary Representation of Real or (Floating Point numbers)
Binary
Decimal
1 1 1 . 1 0 1 = 22 + 21+ 20+ 2-1+2-2+2-3 = 4+ 2+ 1+ 0 +.25 + 0 + .0625 = [7 .3125]
Normalized
Shift up to 126 times until the result is in the form 1. -----------,
( or 127 times if no 1 is found with fewer shifts so there is 0.-------- )
1 1 1 . 1 0 1--Normalize--> 1. 1 1 1 0 1 x 22(decimal)= 10 in binary
The number to be represented is a binary number M. The is number has the form
+or- 1.x22x21..............x1x0 x 2n=e - 0111 1111
32 31 ...
23 22
........
0
x
x
............x
x
x
x
x
+=0 n + 0011 1111
22 21
4 3 2 1 0
-=1
=f
(xj= 1 or 0)
=e
Positive Number Sign-Magnitude
Maximum e = 1111 1110 (1111 1111 is OK but requires special explanation)
= +28-1- 1= 254
Maximum n = Maximum e - 0111 1111 = 1111 1110 - 0111 1111 =
[0|1111 1110]
+ [1|1000 0001] - 127 in 2's Complement
[0|0111 1111]
Result is positive
= (28-2) - ( 27-1)= 28-2 - 27+1)= 2x27- 27- 1
Maximum n = 27- 1=127
Minimum e = 0000 0001 (0000 0000 is OK but requires special explanation)
=1
Minimum n = minimum e - 111 1111 = 0000 0001 - 0111 1111
= [0|0000 0001]
+ [1|1000 0001] = -127 in 2's Complement
= [1|1000 0010]
Result is Negative So 2's Comlement fo Magnitude
= [0 |0111 1110] )
Minimum n = - 126
M= 263
M = 1000 0 111
EXAMPLES
0111 0000
11 0000 0000 0000 0000 0000
f
e
n = e - 127 = 112 - 127= - 15 1+f = 1.11 =20+2-1+2-2 = 1.75
0
[0|0111 0000]
[1|1000 0001] = -127
[1|1111 0001] ----> [0|0000 1111]
M= 1.75 x 2- 15
M = .0 0000 0000 0000 0111
© M C Paull 2006
M = 1. 00 0111 x 26
f = 00 0111
n =6 = 0110
e = 6 + 127 = 0000 0110
+ 0111 1111
1000 0101
0
6
1000 0101
e
00 0111 0000 0000 0000 0000
f
!
1 2 3
A
C
“
B
sp
ASCII Code For Transmission Keyboard To CPU
Sample Key Codes
Key H H
H1H0
1
0
sp 0010 0000 3 2 2 0
! 0010 0001 3 3 2 1
“ 0010 0010 3 4 2 2
1
2
3
0011 0001 4 9 3 1
0011 0010 50 3 2
0011 0011 51 3 3
A
B
C
0100 0001 65
0100 0010 65
0100 0011 66
Binary
4 1
4 2
4 3
Decimal HexaDecimal
Representing Binary Codes
Binary Encoding Of Characters, and Other Special Key Boared Symbols
© M C Paull 2006
7
APPENDIX
PROPERTIES OF 2's COMPLEMENT- JUSTIFICATIONS
TC[M]
01101100
com
1's
m
ple
ent
1's
00010011
+1
1101100
M
sub
nt
trac
me
e
l
t fro
p
m
o
m
c
1 0 0 0 0 0 0 0 = 2n
- 1101100- M
0010011
+1
10010011
2n
0 1 0 1 0 0 2's complement of M
0010100
+ 1 0 0 0 0 0 0 0 = 2n
10010100
add 1 in the sign bit
10010100
TC[-M]
2‘s complement of -M
01101100
TC[M] is the 2's complement of positive integer M It results in the representation of integer -M
using n+1 bits, [n...0 ].
Bit n is th sign bit, 0 for positive and 1 for negative numbers.
TC[M] is easily computed by complementing M and adding 1. The following alternative
definition is basic to the subsequent development.
TC[ M] = (2n - M), where the first 2n makes the sign bit 1. This is the same as subtracting
M from all 0s in bits 0 through n
Note that TC[TC[M]] = TC [M] = TC[2s complement representation of -M] = 2n+ 2n - [2n+ (2n - M)] =
the representation of + M
-0 is represented by TC[0] = 2n - 0 = 2n ( [10...0 ] )
+0 is represented by TC[TC[0] ] = TC[2n ] = 2n - 2n= 0
Since we have two representations of 0 we make the representation of -0 = ( [10...0 ] ),
namely 2n represent - 2n = The most negative Integer
-m, 1 =< m =< 2n - 1, is represented by TC[ m] = (2n - m)
ex. (2n - 1) if m=1( [01...1 ] ), and 1 if m = 2n - 1 ( [00...1 ] )
+m, 1 =< m =< 2n - 1, is represented by TC[TC[ m]] = 2n - (2n - m) = m
ex. 1 if m=1( [00...01 ] ), and 2n - 1 if m = 2n - 1 ( [01...1 ] ) = the highest Positive Integer
© M C Paull 2006
9
OVERFLOW
Two Positive Numbers:
Consider the addition of two n bit positive (bit n is 0 numbers of magnitude M and N in 2's complements
representation .It is clear that staightforward binary addition of M and N , so represented, will give the correct
representation of the sum as long as the sign bit of the result is 0. If the sign bit is 1 then there has been overflow
the result is larger than can be represented with the n+1 bit available.
There is Overflow iff there is a 0 in the sign bit.
Two Negative Numbers:
Consider the addition of the n+1 bit negative integers of magnitude M and N in 2's complement
representation, both within acceptable range ex.
TC[ M] = 2n + (2n - M)
TC[ M] = 2n + (2n - N)
Simply adding TC[M] and TC[N] gives
TC[M] + TC[N] = 2n + (2n - M) + 2n + (2n - N)
= 2n + 2n + 2n + 2n - ( M+N))
= 2n+2 + 2n + ( 2n - ( M+N) )
=
2n + ( 2n - ( M+N) ) since is outside the representation [n...0]
But
TC[M] + TC[N] = TC[M+N] = 2n + ( 2n - ( M+N) )
If 2n < (M + N) then 2n < 2n + (2n - (M + N)) there is a 0 in position n, the sign bit, and there is overflow
If 2n < (M + N) then 2n >= 2n + (2n - (M + N)) there is a 1 in position n, the sign bit, and there is no overflow
There is Overflow iff there is a 1 in the sign bit.
© M C Paull 2008
10
Download