Binary Conversion Addition and Negative Number Representation

advertisement
Digital Systems with Lab
ENGIN 241
Lecture 3 – Binary Conversion, Addition, and
Negative Number Representation
Dr. Filip Cuckov
Engineering Program
University of Massachusetts Boston
Unsigned Binary Number
• The value is calculated from the power series
using the digits from the unsigned binary
number
2
Range of numbers
• Binary number: i.e, 3-bit number: n=3
– 000, 001 … ,111 or in decimal system: 0, 1 … 7
• Total of 8 numbers (=23)
• Range: from 0 to 7 (0 to 23-1)
– In general a n-bit number represents:
• 2n different numbers
• Min: 0
• Max number: 2n-1
• For fractions: m bits after the radix point:
– Min: 0
– Max number: (2m -1)/2m
3
Use of HEX system
• Short hand notation of large binary numbers:
– Each HEX digits can be represented by exactly 4 bits
(16=24)
– Thus (10011110.0101) 2
9
E . 5
• Conversion from binary to HEX and HEX to binary
is very easy:
(10011101)2 = (
)16
(1010110110.11)2 = (
)16
B39.716 = (
)2
4
Conversion Between Bases
• To convert from one base to another:
1. Convert the Integer Part
2. Convert the Fraction Part
3. Join the two results with a radix point
5
From Decimal to new Radix
• To Convert the Integral Part:
Repeatedly divide the number by the new radix and
save the remainders. The digits for the new radix are
the remainders in reverse order of their computation.
• To Convert the Fractional Part:
Repeatedly multiply the fraction by the new radix and
save the integer digits that result. The digits for the new
radix are the integer digits in order of their computation.
If the new radix is > 10, then convert all integers > 10 to
digits A, B, …
6
Example: convert (325.65)10 to hex
• Integer part: 32510 = (
325/16 = 20 and rem = 5
20/16 = 1 and rem = 4
1/16 = 0 and rem = 1
• Fractional part: .65
0.65x16 = 10.4 thus int = 10= A
0.4x16 = 6.4 thus int = 6
0.4x16 = 6.4 thus int = 6
Etc.
325.6510 = 145.A6616
. )16
Least significant digit
Most significant
Thus 32510 = 14516
Most significant
Least significant
Thus .6510 = A6616
7
Addition
• Decimal
11
3734
+ 5168
8902
carries
• Binary
11
1011
+ 0011
1110
carries
8
Binary Addition Examples
• Add the following 4-bit binary
numbers
• Add the following 4-bit binary
numbers
1001
+ 0101
1011
+ 0110
9
Binary Addition Examples
• Add the following 4-bit binary
numbers
• Add the following 4-bit binary
numbers
Overflow! How do we deal with this? 
1
1001
+ 0101
1110
111
1011
+ 0110
10001
10
Overflow
• Digital systems operate on a fixed number of bits
• Addition overflows when the result is too big to fit in
the available number of bits
• Previous example
11
Signed Binary Numbers
• Sign/Magnitude Numbers
• One’s Complement
• Two’s Complement Numbers
12
Sign/Magnitude Numbers
• 1 sign bit, N-1 magnitude bits
• Sign bit is the most significant (left-most) bit
– Positive number: sign bit = 0
– Negative number: sign bit = 1
• Example, 4-bit sign/mag representations of ± 6:
+6 = 0110
- 6 = 1110
• Range of an N-bit sign/magnitude number:
[-(2N-1-1), 2N-1-1]
13
Sign/Magnitude Numbers
• Problems:
– Addition doesn’t work, for example -6 + 6:
1110
+ 0110
10100 (wrong!)
– Two representations of 0 (± 0):
1000
0000
14
Two’s Complement Numbers
• Don’t have same problems as sign/magnitude
numbers:
– Addition works
– Single representation for 0
15
Two’s Complement Numbers
• Flip the sign of a two’s complement number
Method:
– Invert the bits
– Add 1
• Example: Flip the sign of 310 = 00112
16
Two’s Complement Numbers
• Flip the sign of a two’s complement number
Method:
– Invert all the bits
– Add 1
• Example: Flip the sign of 310 = 00112
1. 1100
2. + 1
1101 = -310
17
Two’s Complement Numbers
• Same as unsigned binary, but the most significant bit
(msb) has value of -2N-1
n 2
• Aa
2n 1  a 2i
n 1

 
i 0
i
• Most positive 4-bit number:
• Most negative 4-bit number:
• The most significant bit still indicates the sign
(1 = negative, 0 = positive)
• Range of an N-bit two’s comp number:
18
Two’s Complement Numbers
• Same as unsigned binary, but the most significant bit
(msb) has value of -2N-1
n 2
• Aa
2n 1  a 2i
n 1

 
i 0
i
• Most positive 4-bit number: 0111
• Most negative 4-bit number: 1000
• The most significant bit still indicates the sign
(1 =
negative, 0 = positive)
• Range of an N-bit two’s comp number: [-(2N-1), 2N-1-1]
19
Two’s Complement Examples
•
Take the two’s complement of 610 = 01102
•
What is the decimal value of the two’s
complement number 10012?
20
Two’s Complement Examples
•
•
Take the two’s complement of 610 = 01102
1. 1001
2. + 1
10102 = -610
What is the decimal value of the two’s
complement number 10012?
1. 0110
2. + 1
01112 = 710, so 10012 = -710
21
Two’s Complement Addition
•
Add 6 + (-6) using two’s complement numbers
0110
+ 1010
•
Add -2 + 3 using two’s complement numbers
1110
+ 0011
22
Two’s Complement Addition
•
•
Add 6 + (-6) using two’s complement numbers
111
0110
+ 1010
10000
Add -2 + 3 using two’s complement numbers
111
1110
+ 0011
10001
23
Detecting Overflow:
• Assuming 4-bit two’s complement numbers,
one can detect overflow by detecting when the
two numbers’ sign bits are the same but are
different from the result’s sign bit
• If the two numbers’
sign bits are different,
impossible.
overflow is
24
Detecting Overflow:
• Even simpler method: Detect difference
between carry-in to sign bit and carry-out
from sign bit
25
Increasing Bit Width
•
A value can be extended from N bits to M bits
(where M > N) by using:
 Sign-extension
 Zero-extension
26
Sign-Extension
•
•
Sign bit is copied into most significant bits.
Number value remains the same.
•
Example 1:
– 4-bit representation of 3 = 0011
– 8-bit sign-extended value: 00000011
• Example 2:
– 4-bit representation of -5 = 1011
– 8-bit sign-extended value: 11111011
27
Zero-Extension
•
•
Zeros are copied into most significant bits.
Value will change for negative numbers.
•
Example 1:
– 4-bit value = 00112 = 310
– 8-bit zero-extended value: 00000011 = 310
• Example 2:
– 4-bit value = 1011 = -510
– 8-bit zero-extended value: 00001011 = 1110
28
Number System Comparison
Number System
Range
Unsigned
[0, 2N-1]
Sign/Magnitude
[-(2N-1-1), 2N-1-1]
Two’s Complement
[-2N-1, 2N-1-1]
For example, 4-bit representation:
-8
-7
-6
-5
-4
-3
-2
-1
Unsigned
0
1
2
3
4
5
6
7
9
10
11
12
13
14
15
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111
1111 1110 1101 1100 1011 1010 1001
8
0000
1000
0001 0010 0011 0100 0101 0110 0111
Two's Complement
Sign/Magnitude
29
Book Reference
• Chapters 13 and 14
30
Download