110 likes | 122 Views
Learn about addition and subtraction in computer math, including binary numbers and hex calculations. Understand the rules and carry values for accurate computations.
E N D
CPS120: Introduction to Computer Science Computer Math: Addition and Subtraction
Addition & Subtraction Terms • A + B • A is the augend • B is the addend • C – D • C is the minuend • D is the subtrahend
Addition Rules – All Bases Addition • Step 1: Add a column of numbers • Step 2: Determine if there is a single symbol for the result • Step 3: If so, write it and go to the next column. If not, write the accompanying number and carry the appropriate value to the next column
Addition of Binary Numbers • Rules for adding or subtracting very similar to the ones in decimal system • Limited to only two digits • 0 + 0 = 0 • 0 + 1 = 1 • 1 + 0 = 1 • 1 + 1 = 0 carry 1
Inputs: A B Carry Sum 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 The Carry output is a simple AND function, and the Sum is an Excusive-OR. Thus, we can use two gates to add these two bits together. The resulting circuit is shown below. Half-Adder
Arithmetic in Binary Remember: there are only 2 digits in binary: 0 and 1 Position is key, carry values are used: Carry Values 1 1 1 1 1 1 1 0 1 0 1 1 1 +1 0 0 1 0 1 1 1 0 1 0 0 0 1 0 14
Inputs: A B Cin Cout S 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 We can use two half-adder circuits. The first will add A and B to produce a partial Sum, while the second will add CIN to that Sum to produce the final S output. If either half-adder produces a carry, there will be an output carry. Thus, COUT will be an OR function of the half-adder Carry outputs. The resulting full adder circuit is shown below. Full-Adder
Number Overflow • Overflow occurs when the value that we compute cannot fit into the number of bits we have allocated for the result. For example, if each value is stored using eight bits, adding 127 to 3 overflow: 01111111 + 00000011 10000010 • Overflow is a classic example of the type of problems we encounter by mapping an infinite world onto a finite machine.
Subtraction Rules – All Bases • Step1: Start with the rightmost column, if the column of the minuend is greater than that of the subtrahend, do the subtraction, if not… • Step 2: Borrow one unit from the digit to the left of the once being processed • The borrowed unit is equal to “borrowing” the radix • Step 4: Decrease the column form which you borrowed by one • Step 3: Subtract the subtrahend from the minuend and go to the next column
Subtracting Binary Numbers Remember borrowing? Apply that concept here: 1 2 2 0 2 1 0 1 0 1 1 1 - 1 1 1 0 1 1 0 0 1 1 1 0 0 15
Addition & Subtraction of Hex • Due to the propensity for errors in binary, it is preferable to carry out arithmetic in hexadecimal and convert back to binary • If we need to borrow in hex, we borrow 16 • It is convenient to think “in decimal” and then translate the results back to hex