I'm following a course on Computer Architecture and I have the following exercise:
What is the smallest positive decimal number that should be added to -20 so that a carry-out is triggered (in 2 complement's notation)?
I solved this exercise, but I'm not sure of my results. So I found this:
First convert -20 to 2 complements binary notation. This gives -20 = 11101100. Now add 000101000 to trigger a carry out. Which is equivalent to 20.
Would that be correct?
Yes! That is correct!
Adding -20
to 20
will give you 0
as a result with a carry-out 1
. If you choose any positive number smaller than 20
it won't generate a carry-out and the result will be a negative number. If you choose any positive number larger than 20
it will generate a carry-out with a positive result. So 20
is the answer you are looking for.
If you really want to go into the binary detail, you can go step-by-step:
-20
and will generate a carry-out, let's represent it with x
, y
will be the result and c
will be the carry-out. 11101100
+ xxxxxxxx
--------
cyyyyyyyy
c = 1
and we know that X is positive
so its most significant bit is 0
11101100
+ 0xxxxxxx
--------
1yyyyyyyy
y[6] = 0
. 11101100
+ 0xxxxxxx
--------
10yyyyyyy
6
. Either x[6]
is one. Or there was a carry-in from bit 5. We want to minimize x
, so we should choose x = 0
with a carry-in from bit 5
. 11101100
+ 00xxxxxx
--------
10yyyyyyy
5
. We can choose between x[5] = 1
or a carry-in from bit 4
. We should go for carry-in from bit 4
, since we have to minimize x
. 11101100
+ 000xxxxx
--------
10yyyyyyy
x[4] = 1
because if x[4] = 0
there will be no carry being propagated to higher bits. 11101100
+ 0001xxxx
--------
10yyyyyyy
x[3] = 0
to minimize x
. 11101100
+ 00010xxx
--------
10yyyyyyy
1
on the operand. It is our last chance to generate a carry. If we choose x = 0
there will be no carry out. Our only option is x = 1
. 11101100
+ 000101xx
--------
10yyyyyyy
x
should be 0
, because once again we are minimizing x
. 11101100
+ 00010100
--------
10yyyyyyy
11111
11101100
+ 00010100
--------
100000000
Ta-da!