Lecture 2 More number systems/complements. The main contents of the chapter consist of the following: Hexadecimal numbers; related to binary and octal numbers; conversion between hexadecimal, octal and binary; value ranges of numbers; representing positive and negative numbers; creating the complement of a number.
Trang 1Digital Logic Design
Lecture 2
More Number Systems/Complements
Trang 2w
° Hexadecimal numbers
• Related to binary and octal numbers
° Conversion between hexadecimal, octal and binary
° Value ranges of numbers
° Representing positive and negative numbers
° Creating the complement of a number
• Make a positive number negative (and vice versa)
° Why binary?
Trang 3Understanding Binary Numbers
° Binary numbers are made of binary digits (bits):
Trang 4° Note that each hexadecimal digit can be represented
with four bits.
• (1110) 2 = (E) 16
° Groups of four bits are called a nibble.
• (1110) 2
Trang 5° Possible to convert
between the three formats
Trang 6Converting Between Base 16 and Base
2
° Conversion is easy!
Determine 4-bit value for each hex digit
° Note that there are 2 4 = 16 different values of four
bits
° Easier to read and write in hexadecimal
° Representations are equivalent!
3A9F16 = 0011 1010 1001 11112
Trang 7Converting Between Base 16 and Base
8
1 Convert from Base 16 to Base 2
2 Regroup bits into groups of three starting from
right
3 Ignore leading zeros
4 Each group of three bits forms an octal digit.
Trang 9Decimal 2 Binary
Trang 10Number systems (Octal Numbers )
°Octal number has base 8
°Each digit is a number from 0 to 7
°Each digit represents 3 binary bits
°Was used in early computing, but was replaced by hexadecimal
Trang 11Decimal to Octal Conversion:
The Division:
[359] 10 = [ ? ] 8
By using the division system:
08
5
58
44
448
Trang 12Binary to Octal Conversion:
Example:-
[110101] 2 = [ ? ] 8
Here we will take 3 bits and convert it from binary to
decimal by using the decimal to binary truth table:
Trang 13Octal to Binary Conversion:
Example:-
[13] 8 = [ ? ] 2
Here we will convert each decimal digit from decimal
to binary (3 bits) using the decimal to binary truth
Trang 15• Converting to decimal from octal:
– Evaluate the power series
Trang 16° Hexadecimal is used to simplify dealing with large
binary values:
• Base-16, or Hexadecimal, has 16 characters: 0-9, A-F
• Represent a 4-bit binary value: 0000 2 (0) to 1111 2 (F)
• Easier than using ones and zeros for large binary
Trang 17Hex Values in Computers
Trang 19Conversion Binary to Hexadecimal
1 0 1 0 1 1 0 0 0 0 0 1 0 1 1 0
Trang 21Hexadecimals – Base 16
• Converting to decimal from hex:
– Evaluate the power series
Trang 22Octal to Hex Conversion
To convert between the Octal and Hexadecimal
numbering systems
Convert from one system to binary first
Then convert from binary to the new numbering system
Trang 23Hex to Octal Conversion
Ex : Convert E8A16 to octal
First convert the hex to binary:
1110 1000 10102
111 010 001 010 and re-group by 3 bits
(starting on the right) Then convert the binary to octal:
7 2 1 2
So E8A = 7212
Trang 24Octal to Hex Conversion
Ex : Convert 7528 to hex
First convert the octal to binary:
111 101 0102 re-group by 4 bits
000 1 1110 1010 (add leading zeros)
Then convert the binary to hex:
1 E A
So 7528 = 1EA16
Trang 31Example: Hex → Octal
° First convert the hexadecimal number into its
decimal equivalent, then convert the decimal
number into its octal equivalent.
MSB
1 r
0 1 8
3 r
1 11 8
LSB
2 r
11 90 8
Trang 32Fractions (Example)
Trang 33Fractions (Example)
Trang 35° Convert 0.513 10 to base 16 (up to 4 fractional point)
Trang 37How To Represent Signed
Numbers
• Plus and minus sign used for decimal
numbers: 25 (or +25), -16, etc.
• For computers, desirable to represent
everything as bits .
• Three types of signed binary number
representations: signed magnitude, 1’s complement, 2’s complement.
• In each case: left-most bit indicates sign:
positive (0) or negative (1).
Consider signed magnitude:
000011002 = 1210 100011002 = -1210
Trang 38° It has already been studied that
° subtracting one number from another is the same as making
one number negative and just adding them.
° We know how to create negative numbers in the binary
number system.
° How to perform 2’s complement process.
° How the 2’s complement process can be use to add (and
subtract) binary numbers.
° Digital electronics requires frequent addition and subtraction
of numbers You know how to design an adder, but what about
a subtracter?
° A subtracter is not needed with the 2’s complement process
The 2’s complement process allows you to easily convert a
positive number into its negative equivalent.
° Since subtracting one number from another is the same as
Trang 393-Digit Decimal
A bicycle odometer with only
three digits is an example of a
fixed-length decimal number
system.
The problem is that without a
negative sign, you cannot tell a
+998 from a -2 (also a 998) Did
you ride forward for 998 miles
or backward for 2 miles?
Note: Car odometers do not work
this way.
999 998 997
001 000 999 998
002
forward (+)
backward (-)
Trang 40Negative Decimal
How do we represent negative
numbers in this 3-digit decimal
number system without using a
001 000 999 998
501 500
pos(+)
neg(-)
+499 +498 +497
+001 000 -001 -002
-499 -500
Trang 413-Digit Decimal (Examples)
3
+ 2
5
003 + 002
997
6 + (-3)
3
(-2) + (-3) (-5)
006 + 997
1 003
Disregard Overflow
998 + 997
1 995
Disregard
Trang 42Complex Problem
° The previous examples demonstrate that this
process works, but how do we easily convert
a number into its negative equivalent?
° In the examples, converting the negative
numbers into the 3-digit decimal number
system was fairly easy To convert the (-3),
you simply counted backward from 1000 (i.e.,
999, 998, 997).
° This process is not as easy for large numbers
(e.g., -214 is 786) How did we determine this?
° To convert a large negative number, you can
use the 10’s Complement Process
Trang 43The 10’s Complement process uses base-10
(decimal) numbers Later, when we’re working
with base-2 (binary) numbers, you will see that
the 2’s Complement process works in the same
way.
First, complement all of the digits in a number
• A digit’s complement is the number you add to the
digit to make it equal to the largest digit in the base
(i.e., 9 for decimal) The complement of 0 is 9, 1 is 8,
2 is 7, etc.
Second, add 1.
• Without this step, our number system would have
two zeroes (+0 & -0), which no number system has.
Trang 4410’s Complement Examples
-003
+1
996 997 -214
Trang 45One’s Complement
Representation
• The one’s complement of a binary number
involves inverting all bits.
• 1’s comp of 00110011 is 11001100
• 1’s comp of 10101010 is 01010101
• For an n bit number N the 1’s complement is
(2 n -1) – N.
• Called diminished radix complement by Mano
since 1’s complement for base (radix 2).
• To find negative of 1’s complement number
take the 1’s complement.
000011002 = 1210 111100112 = -1210
Trang 46Two’s Complement
Representation
• The two’s complement of a binary number
involves inverting all bits and adding 1
• 2’s comp of 00110011 is 11001101
• 2’s comp of 10101010 is 01010110
• For an n bit number N the 2’s complement is
(2 n -1) – N + 1.
• Called radix complement by Mano since 2’s
complement for base (radix 2).
• To find negative of 2’s complement number
take the 2’s complement.
000011002 = 1210 111101002 = -1210
Trang 47Two’s Complement
Shortcuts
° Algorithm 1 – Simply complement each bit and
then add 1 to the result.
• Finding the 2’s complement of (01100101) 2 and of its 2’s
complement…
N = 01100101 [N] = 10011011
10011010 01100100
+ 1 + 1
-
10011011 01100101
° Algorithm 2 – Starting with the least significant bit, copy all of the bits up to and including the first 1
bit and then complementing the remaining bits.
[N] = 1 0 0 1 1 0 1 1
Trang 48° Machines that use 2’s complement arithmetic can
represent integers in the range
-2 n-1 <= N <= 2 n-1 -1 where n is the number of bits available for
representing N Note that 2 n-1 -1 = (011 11) 2 and –2 n-1 = (100 00) 2
oFor 2’s complement more negative numbers than
positive.
oFor 1’s complement two representations for zero.
oFor an n bit number in base (radix) z there are z n
different unsigned values.
(0, 1, …z n-1 )
Trang 49Add carry
Final Result
Step 1: Add binary numbers
Step 2: Add carry to low-order bit
Add
Trang 501’s Complement
Subtraction
° Using 1’s complement numbers, subtracting
numbers is also easy
° For example, suppose we wish to subtract
Add carry Final
Step 1: Take 1’s complement of 2 nd operand
Step 2: Add binary numbers
Step 3: Add carry to low order bit
1’s comp Add
Trang 51Step 1: Add binary numbers
Step 2: Ignore carry bit
Add
Ignore
Trang 52Step 1: Take 2’s complement of 2 nd operand
Step 2: Add binary numbers
Step 3: Ignore carry bit
2’s comp Add
Ignore
Trang 532’s Complement Subtraction: Example
#2
° Let’s compute (13) 10 – (5) 10
• (13) 10 = +(1101) 2 = (01101) 2
• (-5) 10 = -(0101) 2 = (11011) 2
° Adding these two 5-bit codes…
° Discarding the carry bit, the sign bit is seen to be zero, indicating a correct result Indeed,
Trang 542’s Complement Subtraction: Example
#3
° Let’s compute (5) 10 – (12) 10
• (-12) 10 = -(1100) 2 = (10100) 2
• (5) 10 = +(0101) 2 = (00101) 2
° Adding these two 5-bit codes…
° Here, there is no carry bit and the sign bit is 1
This indicates a negative result, which is what we expect (11001) 2 = -(7) 10
0 0 1 0 1
-1 -1 0 0 -1
Trang 551’s Complement
Trang 562’s Complement
• 1s complement with negative numbers shifted one
position clockwise
• Only one representation for 0
• One more negative number than positive number
• High-order bit can act as sign bit
Trang 57° Overflow conditions
° Add two positive numbers to get a negative number
° Add two negative numbers to get a positive number
Trang 58Two’s Complement Number System
0000
0001
0010
0011 0100
0101 0110
1010 1011
1100 1101
Trang 59° Signed numbers represented in signed magnitude, 1’s
complement, and 2’s complement
° 2’s complement most important (only 1 representation
for zero).
° Important to understand treatment of sign bit for 1’s
and 2’s complement.