330 likes | 473 Views
Cosc 235: Computer Organization. Binary Arithmetic. Objectives. Add with binary numbers Represent negative binary numbers in one’s and two’s compliments Perform 3 methods of subtraction with binary numbers Multiply and divide with binary numbers
E N D
Cosc 235: Computer Organization Binary Arithmetic
Objectives • Add with binary numbers • Represent negative binary numbers in one’s and two’s compliments • Perform 3 methods of subtraction with binary numbers • Multiply and divide with binary numbers • Subtract decimal numbers by nine’s compliment
Addition Add 2 numbers, A and B, to get the sum, S. A A3A2A1A0 + B +B3B2B1B0 S C S3S2S1S0 Any base: add the digits corresponding to the same power • Carry
Addition • The sum of A0 and B0 produce 2 outputs: • Sum, S, the portion of A0 + B0 that will fit in the 0th position • Carry, C, the portion of A0 + B0 that will not fit in the 0th position and must be considered in the 1st position
Truth Table for Binary Addition A0 B0 C1 S0 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0
Non-LSB Addition • If we are not at the LSB of the numbers, we have an additional input: the carry from the previous digit • How many digits do we need to handle a 3-input binary addition?
Truth Table for Binary Addition Ci Ai Bi Ci+1 Si 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1
Fractional addition 0.01012 = 5/1610 0.01102 = 6/1610 --------- ------- 0.10112 = 11/1610
Storing negative numbers • Sign + magnitude • For base n, we have (n-1)-complement • Representation scheme • Subtract each digit in the negative number from (n-1) • Example: 9’s compliment for base 10 • Example: 1’s complement for base 2 • Binary case: 2’s complement
Nine’s Compliment • The Nine’s Compliment is sometimes used in operations with BCD • 9’s compliment is obtained by subtracting a decimal number from all nines A = 764 A = 23.675 A = 999 A = 99.999 -764-23.675 235 76.324
One’s compliment Pad with leading 0’s for a specific word size • Compliment each bit • Complimenting: change 0 to 1, 1 to 0
Binary Subtraction Algorithm • Obtain the 1’s compliment of the subtrahend • Add • Take the MSB carry and add back into the results at the LSB • “End-around carry” • If EAC==0, B>A and result is negative • Compliment result to verify value _ • A - B => A + B + EAC
One’s Compliment Example A 13 1101 -B-10-1010 A 1101 +B +0101 (1)0010 + 1 (EAC) 0011
Fractional One’s Compliment A = 28/32 = .11100 -X = 17/32 = .10001 A .11100 +X+ .01110 (1).01010 + 1 (EAC) .01011 = 11/32
Two’s Compliment • Adding the EAC is cumbersome • Skip this step: use a different storage structure • Two’s Compliment is the One’s compliment + 1
Two’s Compliment: methods for obtaining • A = 2110 = 0101012 A1 = 101010 + 1.. 1010112 = A2 • If X = next power of 2 > A, then set all bits left of and including X. right of X, set bits to X – A • Compliment only bits to the left of the least significant 1
Subtraction using Two’s Compliment • If the end carry = 1, that indicates a positive result, and the end carry is ignored • If the end carry = 0, that indicates a negative result, and the end carry is ignored • Negative result is in Two’s Compliment form • To verify, take the 2’s compliment of the result
Fractional Two’s Compliment Subtraction 10/1610 = .10102 - 7/1610 = -.01112 .1010 +.1001 (1).0011 = 3/1610
Binary Multiplication • Simpler than decimal multiplication • Only need to shift the multiplicand and add
Example Binary multiplication 00101 multiplicand x 00101 multiplier ------- 00101 multiply by 1 00000 shift and multiply by 0 +00101 shift and multiply by 1 (etc) -------- 0011001 = 24 + 23 + 20 = 25
Another example 011010 multiplicand = 2610 x 001010 multiplier = 1010 -------- 000000 multiply by 0 011010 shift & multiply by 1 000000 shift & multiply by 0 011010 shift & multiply by 1 ---------- 0100000100 28 + 22 = 26010
Binary division • Like multiplication, binary division is easier than decimal division • Our quotient bits can either be a 0 or a 1, not a multiple of the divisor
Summary • How the arithmetic operations are performed in binary • Addition • Subtraction • Multiplication • Division
Glossary • One’s Compliment The One’s compliment of a binary number is accomplished by converting all 1’s to 0 and all 0’s to 1 • Two’s Compliment One’s compliment + 1 • Nine’s Compliment Decimal number resulting from the subtraction of a number from all 9’s.
Glossary • End Carry A carry having significance greater than the MSB of either term in an addition • End Around Carry End carry generated during one’s compliment subtraction is taken around and added to the LSB of the sum