1 Digital Logic Design Lecture 2 More Number Systems/Complements
2 Overview °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?
3 Understanding Binary Numbers °Binary numbers are made of binary digits (bits): 0 and 1 °How many items does an binary number represent? (1011) 2 = 1x x x x2 0 = (11) 10 °What about fractions? (110.10) 2 = 1x x x x x2 -2 °Groups of eight bits are called a byte ( ) 2 °Groups of four bits are called a nibble. (1101) 2
4 Understanding Hexadecimal Numbers °Hexadecimal numbers are made of 16 digits: (0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F) °How many items does an hex number represent? (3A9F) 16 = 3x x x x16 0 = °What about fractions? (2D3.5) 16 = 2x x x x16 -1 = °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
5 Putting It All Together °Binary, octal, and hexadecimal similar °Easy to build circuits to operate on these representations °Possible to convert between the three formats
6 Converting 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! 3A9F 16 = A9F
7 Converting 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 = A9F 16 = A9F
8
9 Decimal 2 Binary
10 Octal Numbers: [Base 8],[ 0,1,3,4,5,6,7] Octal to Decimal Conversion: Example:- [2374] 8 = [ ? ] 10 =4×8 0 +7×8 1 +3×8 2 +2×8 3 =[1276] 10 Number systems () Number 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
11 Decimal to Octal Conversion: The Division: [359] 10 = [ ? ] 8 By using the division system: Reminder Number systems () Number systems (Octal Numbers) Quotient
12 Binary 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: BinaryDecimal = (65) 8 { 65 { Number systems () Number systems (Octal Numbers)
13 Octal 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 table: BinaryDecimal (13) 8 = (001011) 2 Number systems () Number systems (Octal Numbers)
14 Radix Based Conversion (Example) °Convert 1234 decimal into octal Radix 8Answer Divide by radix
15 Octals Converting to decimal from octal: –Evaluate the power series Example *8 1 2* *8 0 + =
16 Hexadecimal °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: (0) to (F) Easier than using ones and zeros for large binary values Commonly used in computer applications °Examples: = = C = A6 C2 16 Hex values can be followed by an “H” to indicate base-16. Example: A6 C2 H
17 Hex Values in Computers
18 Decimal to Hexadecimal DecimalHex A 11B 12C 13D 14E 15F
19 Conversion Binary to Hexadecimal = = = = 6 AC16
20 Radix Based Conversion (Example) °Convert 1234 decimal into hexadecimal Radix 16Answer 4D2 16 Divide by radix D
21 Hexadecimals – Base 16 Converting to decimal from hex: –Evaluate the power series Example 2 E A *16 1 2* * =
22 Octal 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
23 Hex to Octal Conversion Ex : Convert E8A 16 to octal First convert the hex to binary: and re-group by 3 bits (starting on the right) Then convert the binary to octal: So E8A 16 =
24 Octal to Hex Conversion Ex : Convert to hex First convert the octal to binary: re-group by 4 bits (add leading zeros) Then convert the binary to hex: 1 E A So = 1EA 16
25 Octal to Hexadecimal Hexadecimal DecimalOctal Binary
26 Octal to Hexadecimal °Technique Use binary as an intermediary
27 Example = ? E = 23E 16
28 Hexadecimal to Octal Hexadecimal DecimalOctal Binary
29 Hexadecimal to Octal °Technique Use binary as an intermediary
30 Example 1F0C 16 = ? 8 1 F 0 C F0C 16 =
31 Example: Hex → Octal Example: °Convert the hexadecimal number 5A H into its octal equivalent. 5A H = A = Solution: °First convert the hexadecimal number into its decimal equivalent, then convert the decimal number into its octal equivalent.
32 Fractions (Example)
33 Fractions (Example)
34 Radix Based Conversion (Example) °Convert decimal into binary Radix 2 Answer FractionRadixTotal (Fraction x Radix)IntegerFraction Multiply by radix 2
35 FractionRadix Total (Fraction x Radix)IntegerFraction Radix Based Conversion (Example) °Convert to base 16 (up to 4 fractional point) Radix 16 Answer Multiply by radix 16
36
37 How 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: = Sign bitMagnitude = Sign bitMagnitude
38 Complement °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 making one number negative and adding, the need for a subtracter circuit has been eliminated.
39 3-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 forward (+) backward (-)
40 Negative Decimal How do we represent negative numbers in this 3-digit decimal number system without using a sign? Cut the number system in half. Use 001 – 499 to indicate positive numbers. Use 500 – 999 to indicate negative numbers. Notice that 000 is not positive or negative pos(+) neg(-)
41 3-Digit Decimal (Examples) (-5) + 2 (-3) (-3) 3 (-2) + (-3) (-5) 003 Disregard Overflow 995 Disregard Overflow It Works!
42 Complex 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.
43 Complement The 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.
44 10’s Complement Examples 785 Example #1 Example #2 Complement DigitsAdd 1Complement DigitsAdd 1 786
45 One’s Complement Representation The one’s complement of a binary number involves inverting all bits. 1’s comp of is ’s comp of is 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 = Sign bitMagnitude = Sign bitMagnitude
46 Two’s Complement Representation The two’s complement of a binary number involves inverting all bits and adding 1. 2’s comp of is ’s comp of is 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 = Sign bitMagnitude = Sign bitMagnitude
47 Two’s Complement Shortcuts °Algorithm 1 – Simply complement each bit and then add 1 to the result. Finding the 2’s complement of ( ) 2 and of its 2’s complement… N = [N] = °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 = [N] =
48 Finite Number Representation °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 = ( ) 2 and –2 n-1 = ( ) 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 )
49 1’s Complement Addition °Using 1’s complement numbers, adding numbers is easy. °For example, suppose we wish to add +(1100) 2 and +(0001) 2. °Let’s compute (12) 10 + (1) 10. (12) 10 = +(1100) 2 = in 1’s comp. (1) 10 = +(0001) 2 = in 1’s comp Add carry Final Result Step 1: Add binary numbers Step 2: Add carry to low-order bit Add
50 1’s Complement Subtraction °Using 1’s complement numbers, subtracting numbers is also easy. °For example, suppose we wish to subtract +(0001) 2 from +(1100) 2. °Let’s compute (12) 10 - (1) 10. (12) 10 = +(1100) 2 = in 1’s comp. (-1) 10 = -(0001) 2 = in 1’s comp Add carry Final Result 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
51 2’s Complement Addition °Using 2’s complement numbers, adding numbers is easy. °For example, suppose we wish to add +(1100) 2 and +(0001) 2. °Let’s compute (12) 10 + (1) 10. (12) 10 = +(1100) 2 = in 2’s comp. (1) 10 = +(0001) 2 = in 2’s comp Final Result Step 1: Add binary numbers Step 2: Ignore carry bit Add Ignore
52 2’s Complement Subtraction °Using 2’s complement numbers, follow steps for subtraction °For example, suppose we wish to subtract +(0001) 2 from +(1100) 2. °Let’s compute (12) 10 - (1) 10. (12) 10 = +(1100) 2 = in 2’s comp. (-1) 10 = -(0001) 2 = in 2’s comp Final Result Step 1: Take 2’s complement of 2 nd operand Step 2: Add binary numbers Step 3: Ignore carry bit 2’s comp Add Ignore Carry
53 2’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, (01000) 2 = +(1000) 2 = +(8) carry
54 2’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)
55 1’s Complement
56 2’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
57 Overflow °Overflow conditions °Add two positive numbers to get a negative number °Add two negative numbers to get a positive number
58 Two’s Complement Number System
59 Summary °Binary numbers can also be represented in octal and hexadecimal °Easy to convert between binary, octal, and hexadecimal °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.