Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS120: Introduction to Computer Science

Similar presentations


Presentation on theme: "CPS120: Introduction to Computer Science"— Presentation transcript:

1 CPS120: Introduction to Computer Science
Computer Math: Signed Number Operations

2 Two’s Complement Operations
Addition: Treat the numbers as unsigned integers The sign bit is treated as any other number Ignore any carry on the leftmost position

3 One's Complement Operations
Addition Treat the sign bit as any other bit For addition, carry out of the leftmost bit is added to the rightmost bit (end-around carry)

4 Converting Subtraction to Addition
Consider the problem of subtracting 110 from We can also think of this problem as adding -110 to 7

5 Solving the Problem Convert the subtrahend to a negative with either 1's or 2's complementation. Add the negative to the other number. Adjust our answer.

6 Adjusting the Answer The answer is that sometimes the sum in step two will exceed the number of bits in our representation. This is called overflow We handle the extra bit differently in 1's and 2's complement. In 1's complement, we add the overflow bit to our sum to obtain the final answer. In 2's complement, we simply discard the extra bit to obtain the final answer.

7 Subtraction in Two's Complement
Treat the numbers as unsigned integers If a "borrow" is necessary in the leftmost place, borrow as if there were another “invisible” one-bit to the left of the minuend

8 subtracting 110 from 710 using 2's complement.
Convert to its negative equivalent in 2's complement Change all the 1's to 0's and 0's to 1's and add one to the number Add the negative value we computed to Our addition caused an overflow bit. Whenever we have an overflow bit in 2's complement, we discard the extra bit. (7) (1) > 1 (-1) (6)

9 One's Complement Operations
Subtraction Treat the sign bit as any other bit Carry out of the leftmost bit is added to the rightmost bit (end-around carry)

10 subtract 710 from 110 using 1's complement
First, we state our problem in binary. Next, we convert to its negative equivalent and add this to Our result does not cause an overflow, so we do not need to adjust the sum Remember that our answer is in 1's complement notation so the correct decimal value for our answer is -610 (1) (7) (1) (-7) (?) (-6)

11 subtracting 110 from 710 using 1's complement
Convert the subtrahend to its negative equivalent in 1's complement. Change all the 1's to 0's and 0's to 1's. Next, we add the negative value we computed to the other number Addition caused an overflow bit. Whenever we have an overflow bit in 1's complement, we add this bit to our sum to get the correct answer. (7) (1) > (7) (-1) (?) + 1 (6)


Download ppt "CPS120: Introduction to Computer Science"

Similar presentations


Ads by Google