330 likes | 2k Views
0 0 1 0 + 1 1 0 0 = 1 1 1 0. 1. 0 0 1 0 + 1 0 1 0 = 1 1 0 0. 1. 1. 1. 0 1 1 0 + 1 1 1 0 = 1 0 1 0 0. Adding Binary Numbers. Binary addition is similar to decimal addition 0+0 = 0 0+1 = 1 1+0 = 1 1+1 = 10. Carry-Out of bit 1, carry-In to bit 2.
E N D
0 0 1 0 + 1 1 0 0 = 1 1 1 0 1 0 0 1 0 + 1 0 1 0 = 1 1 0 0 1 1 1 0 1 1 0 + 1 1 1 0 = 1 0 1 0 0 Adding Binary Numbers • Binary addition is similar to decimal addition • 0+0 = 0 • 0+1 = 1 • 1+0 = 1 • 1+1 = 10 Carry-Out of bit 1, carry-In to bit 2 • If we have a result greater than 1, we must carry a ‘1’ to the next digit • Produce carry (to left) -> Carry-out • Get a carry (from right) --> Carry-In Carries
X Y Sum CarryOut X 0 0 0 0 Sum + 0 1 1 0 Y 1 0 1 0 1 1 0 1 CarryOut Sum = XY Ú XY X Sum Y CarryOut Half Adder A half adder adds two bits, A and Band produces a Sum and CarryOut = XÅY CarryOut = XY Problem: We often need to add two bits and a CarryIn...
A B CinCoutSum 0 0 0 00 0 0 1 01 0 1 0 01 0 1 1 10 1 0 0 01 1 0 1 10 1 1 0 10 1 1 1 11 CarryIn A Cin Cin Sum 0 0 1 1 + AB AB B 00 00 CarryOut 01 01 B B Sum Cin 11 11 A A Cout Cin 10 10 0 1 1 1 0 0 0 0 1 0 1 1 0 1 1 0 1 Full Adder A full adder adds two bits, A and B, and a CarryIn and produces a Sum and CarryOut ÚBCin ÚACin Cout = AB Sum =A Å B Å Cin
A3A2A1A0+ B3B2B1B0 S3S2S1S0 A1 A2 A0 A3 CIn CIn CIn CIn Sum2 Sum0 Sum1 Sum3 + + + + B2 B0 B3 B1 COut COut COut COut 4-bit Ripple Carry Adder CarryIn=0 Note bit numbering: 3,2,1,0 Connect Cin’s to Cout’sSet global Cin to 0 Global Cout is an output CarryOut
CarryIn=0 A2 A0 A3 A1 CIn CIn CIn CIn Sum1 Sum2 Sum3 Sum0 Full Adder Circuit + + + + A B2 B0 B3 B1 COut COut COut COut Cin B Cout Cin A B A B Sum Cin CarryOut Ripple Carry Timing 0 Inputs stable at 0ns 0 8 5 0 5 5 10 0 18 5 0 20 0 28 8 0 Sum3 stable at 38ns 30 Assume 5ns delay for AND/ORs and 8ns for XORs 0 38 0 Cout stable at 40ns 40
Number Systems- Negative Numbers • What about negative numbers? • We need to represent numbers less than zero as well as zero or higher • In n bits, we get 2n combinations • Make half positive, half negative... First method: use an extra bit for the sign +5 0 1011 101 -5 3 remaining bits for magnitude Sign bit: 0-->positive, 1-->negative
Sign and Magnitude Representation Seven Negative Numbers and “Negative” Zero Note: Example is shown for 4-bit numbers -7 +0 +1 -6 0000 1111 0001 1110 -5 +2 1101 0010 -4 +3 Inner numbers:Binary representation 1100 0011 Seven Positive Numbers and “Positive” Zero -3 1011 +4 0100 1010 0101 -2 +5 1001 0110 +6 -1 1000 0111 +7 -0 • High order bit is sign: 0 = positive (or zero), 1 = negative • Three low order bits represent the magnitude: 0 (000) through 7 (111) • Number range for n bits = +/- 2n-1 -1 • Two different representations for 0! • Two discontinuities
+0 +1 0000 1111 0001 1110 +2 1101 0010 +3 1100 0011 1011 +4 0100 1010 0101 +5 1001 0110 +6 1000 0111 +7 Two’s Complement Representation Re-order Negative Numbers to Eliminate Discontinuities Note: Example is shown for 4-bit numbers -1 -2 -3 -4 Inner numbers:Binary representation Eight Positive Numbers -5 -6 -7 -8 Note: Negative numbersstill have 1 for MSB • Only one discontinuity now • Only one zero • One extra negative number
complement complement complement add 1 add 1 add 1 2’s Complement Negation Method #1 To calculate the negative of a 2’s complement number: WARNING: This is for calculating the negative of a number. There is no such thing as “taking the 2’s complement of a number”. 1. Complement the entire number 2. Add one Examples: n = 0110= 6 n = 01000100 = 68 n = 10010000= -112 1001 10111011 -n =1010 = -6 01101111 -n =10111100 = -68 -n =01110000 = 112
complement complement complement copy copy copy 2’s Complement Negation Method #2 To calculate the negative of a 2’s complement number: 1. Starting at LSB, search to the left for the first one 2. Copy (unchanged) the first one and everything to its right 3. Complement the remaining bits Examples: n = 01000100 = 68 n = 0110= 6 10111 100 -n = = -68 10 10 -n = = -6 n = 10010000= -112 011 10000 -n = = 112
4 + 3 7 0100 0011 0111 -4 + (-3) -7 1100 1101 1 1001 4 - 3 1 0100 1101 1 0001 -4 + 3 -1 1100 0011 1111 Adding Two’s Complement Numbers Just add the completenumbers together. Sign taken care of automatically. Discard carry-out (for now) A carryout from MSB does not necessarily mean overflow!
-1 +0 +1 -2 0000 1111 0001 1110 -3 +2 1101 0010 -4 +3 1100 0011 -5 1011 +4 0100 1010 0101 -6 +5 1001 0110 +6 -7 1000 0111 +7 -8 Overflow Overflow: - Add two positive numbers to get a negative number - Add two negative numbers to get a positive number Not a discontinuity - No Overflow 5 + 3 = -8 -7 - 2 = +7 Overflow occurs when crossing bottom discontinuity A carryout from the MSB could mean crossing at either of these places – One is OK, one is Overflow Overflow cannot occur when adding a positive and negative number together
Cin Cin Cin Cin Cin Cin Cout Cout Cout Cout Cout Cout 0 1 1 0 0 1 0 0 1 1 1 0 0+ 0 0+ 0 1+ 1 1+ 0 1+ 0 1+ 1 Cin Overflow Cout Detecting Overflow Overflow occurs when: We add two positive numbers and obtain a negative We add two negative numbers and obtain a positive Looking at the sign bit (MSB): + + - - - - + + - - + + + - + - - + 0 1 0 1 1 0 No overflow Overflow Overflow No Overflow No Overflow No Overflow Overflow when carryin to sign bit does not equal carryout
Two’s Complement Summary • Two’s complement representations must specify the number of bits • -3 is 1101 in 4-bit, 11111101 in 8-bit, 111111111101 in 12-bit • When adding two’s complement numbers, discard the carryout from MSB • This is NOT overflow it is OK • To represent positive numbers in 2’s complement, just copy them • +2 in 4-bit two’s complement 0010 • To represent negative number in 2’s complement, write out their negative and negate it • -3 is -(+3) - (0011) = 1101
Subtract CarryIn A0 CIn B0 + Sum1 Sum0 Sum2 Sum3 COut A1 CIn B1 + COut A2 CIn B2 + COut A3 CIn B3 + COut CarryOut 4-bit Ripple Carry Adder/Subtractor Subtraction: Adding the negative of the subtracted number In 2’s complement: Negative obtained by: Complementing and adding 1 To subtract A3A2A1A0 - B3B2B1B0: 1. Complement B3B2B1B0 by complementing each one individually (for subtract only) 2. Add one to total by setting global Cin to 1 (for subtract only)