Lecture 4: Digital Systems & Binary Numbers (4) CPE 201 Digital Design Lecture 4: Digital Systems & Binary Numbers (4)
Lecture Outline Signed numbers Encoding techniques
Signed Binary Numbers Cannot use the “minus” sign we use on paper Signed-magnitude MSB keeps the sign: 0 – positive, 1 – negative Hard to use with arithmetic operations The user determines if the number is signed/not 01001 9 (unsigned binary) or +9 (signed binary) 11001 Other examples: 010101012 = + 8510 110101012 = - 8510 011111112 = + 12710 111111112 = - 12710 000000002 = +0 100000002 = -0 Signed complement used for computer arithmetic Signed 1’s complement and signed 2’s complement 25 (unsigned binary) or -9 (signed binary)
Signed Complement Negative number is indicated by its complement Positive numbers start with 0 complement will always start with 1 (negative number) 2’s complement is most common E.g.: represent -9 on eight bits Represent +9: Signed magnitude: Signed 1’s complement: Signed 2’s complement: 00001001 10001001 11110110 11110111
Signed Binary Numbers Decimal Signed 2’s compl. Signed 1’s compl. Signed mag. +7 +6 +5 +4 +3 +2 +1 +0 -0 -1 -2 -3 -4 -5 -6 -7 -8 0111 0111 0111 0110 0110 0110 0101 0101 0101 0100 0100 0100 0011 0011 0011 0010 0010 0010 0001 0001 0001 0000 0000 0000 - 1111 1000 1111 1110 1001 1110 1101 1010 1101 1100 1011 1100 1011 1100 1011 1010 1101 1010 1001 1110 1001 1000 1111 1000 - -
Addition of Signed Numbers Signed magnitude Just like regular arithmetic Same signs: add magnitudes Different signs: subtract smaller magnitude from larger, keep the sign of the larger magnitude Requires comparison and subtraction Complex circuit design Better to use signed complement representation + 24 + 44 = + 68 + 24 + (-44) = - 20
Addition of Signed Numbers Use 2’s complement: Represent negative numbers in 2’s complement Perform addition as usual MUST USE THE SAME NUMBER OF BITS FOR BOTH NUMBERS AND THE SUM
Addition of Signed Numbers Signed 2’s complement: 3+4, -2+(-6), 6+(-3), 4+(-7) +3 0 0 1 1 + -2 1 1 1 1 1 1 1 0 + + +4 0 1 0 0 + -6 1 1 1 1 1 0 1 0 +7 0 1 1 1 -8 1 1 1 1 1 1 0 0 0 Discard carry +6 0 1 1 0 + +4 0 1 0 0 + + -3 1 1 0 1 + -7 1 0 0 1 +3 1 0 0 1 1 -3 1 1 0 1 Discard carry
Overflow Addition can result in overflow if there isn’t enough space available to represent the result Can occur only if operands have the same sign Overflow occurs when the carry bits into and out of the sign position are different E.g.: 0 1 1 1 + 1 1 1 0 +7 + +7 +14 = -2 !! 1 carry into sign bit carry out of 1 1 0 1 1 0 1 0 + 0 1 1 1 -3 + -6 -9 1 carry into sign bit carry out of = 7 !!
Subtraction with Signed 2’s Complement Take the 2’s complement of the subtrahend (including the sign bit) Add it to the minuend (including the sign bit) Ignore any carry out of the sign-bit Make sure to check for overflow A – (+B) = A + (-B) A – (-B) = A + (+B) Equivalent to taking the 2’s complement
Examples: Subtraction Signed 2’s Complement 4 – 3 = 0100 – 0011 = 0100 + 1101 = 10001 3 – 4 = 0011 – 0100 = 0011 + 1100 = 1111 1111 is equal to -1 in 2’s complement 3 – (-4) = 0011 – (-(0100))in 2’s complement = = 0011 – 1100 = 0011 + 0100 = 0111 = 710 -3 – (-4) = 1101 – 1100 = 1101 + 0100 = 10001 = 110 Discard the carry Instead add 2’s complement Discard the carry
Binary Codes Is binary the only way to encode information? Same information can be coded using a different code The encoding still uses 0’s and 1’s E.g.: represent the decimal values 1-9: Binary: 0001 1001 One out of 9: 000000001, 000000010, 000000100, … 010000000, 100000000 A code is a set of n-bit strings, which assigns a unique string to any symbol
Binary Coded Decimal (BCD) Code In BCD, each decimal digit is represented with its binary encoding Easier for people to understand than binary How many bits do we need to represent numbers 0 through 9 in BCD? 4 bits E.g.: (527)10 = (0101 0010 0111)BCD A number with k digits in decimal requires 4k bits in BCD Do not confuse with binary representation! 5 2 7
Addition using BCD code Similar to adding 4-bit unsigned binary numbers A correction must be made if the result exceeds 1001 (9) Addition result range is 0 to 19 (9+9+1carry) Binary: 0000 to 10011 BCD: 0000 to 11001 The difference in result between the BCD and binary values is 6 (110) If the result is 1010 (10 decimal), adding 6 (110) to the result will correct the problem
BCD Addition Example 5 + 9 , 8 + 8, 9 + 9 Results: 5 + 9 = 0101 + 1001 = 1110 (need correction) add 110: 1110 + 110 = 1 0100 (1410) 8 + 8 = 1000 + 1000 = 10000 (need correction) add 110: 10000 + 110 = 1 0110 (1610) 9 + 9 = 1001 + 1001 = 10010 (need correction) add 110: 10010 + 110 = 1 1000 (1810)
Readings Chapter 2 Sections 1.7