Transcript Operations
CPS120: Introduction to Computer
Science
Computer Math:
Signed Number
Operations
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
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)
Converting Subtraction to Addition
Consider the problem of subtracting 110 from
710. We can also think of this problem as
adding -110 to 7
Solving the Problem
1.
2.
3.
Convert the subtrahend to a negative with
either 1's or 2's complementation.
Add the negative to the other number.
Adjust our answer.
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.
Subtraction in Two's Complement
Subtraction
–
–
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
subtracting 110 from 710 using 2's
complement.
1.
Convert 000000012 to its
negative equivalent in 2's
complement
2.
3.
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 000001112
Our addition caused an
overflow bit.
Whenever we have an
overflow bit in 2's
complement, we discard the
extra bit.
00000111
- 00000001
(7)
- (1)
00000001 -> 11111110
1
11111111
00000111
+ 11111111
100000110
(7)
+(-1)
(6)
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)
subtract 710 from 110 using 1's
complement
1.
2.
3.
First, we state our problem in
binary.
Next, we convert 000001112
to its negative equivalent
and add this to 000000012
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
-
-
00000001 (1)
00000111 -(7)
00000001 (1)
+ 11111000 +(-7)
11111001 (?)
-
(-6)
subtracting 110 from 710 using 1's
complement
1.
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.
2.
3.
Whenever we have an
overflow bit in 1's
complement, we add this
bit to our sum to get the
correct answer.
00000111 (7)
- 00000001 - (1)
00000001 -> 11111110
00000111 (7)
+11111110 + (-1)
100000101 (?)
00000101
+1
00000110
(6)