Two's Complement related problem

Thread Starter

vikas11077

Joined Jul 25, 2014
2
Hello Sir,
I want to ask a doubt.
Qes- Consider a 16 bit computer system in which signed number are represented using 2's Complement. 0 represents '+' and 1 represents '-' . Consider two signed numbers A=C91C base 16 and B= F1EE base 16.
(1) A <= B
(2) A >= B
(3) magnitude of A= magnitude of B
(4) Insufficient data
 

MrChips

Joined Oct 2, 2009
30,802
So. Write out the two numbers in binary.
Is the number negative or positive?
What is the 2's complement of a negative number?
Which number has the more positive value?
 

WBahn

Joined Mar 31, 2012
30,052
While you can answer the questions directly, you can also convert both numbers to base 10 and then answer them. So you might give that a try.

You might also work with simpler problems to get a feel for the patterns. For instance,
what if

A = FFFF
B = FFFE

Which answer would apply?

Or write out all of the 4-bit unsigned binary numbers in one column and the 2's complement numbers for the same bit patterns in the next column and see what you can conclude about how to tell if one value is greater than another value.
 

Thread Starter

vikas11077

Joined Jul 25, 2014
2
Hello Mrchips
can you tell me how we will convert the binary number 1000 into decimal. The binary number 1000 is written in 2's complement form.
 

MrChips

Joined Oct 2, 2009
30,802
1000 is a negative number, correct?

So we find the positive value by taking the 2's complement.
How do we do that? We take the 1's complement and add 1.
The 1's complement is 0111.
+1 gives 1000.

Next we convert to decimal.

1000 = 8

Hence the value is -8.
 

WBahn

Joined Mar 31, 2012
30,052
Hello Mrchips
can you tell me how we will convert the binary number 1000 into decimal. The binary number 1000 is written in 2's complement form.
A couple of fine points to note. For signed binary representations it is required that you indicate how many bits are in the representation. Your question implies that it is a 4-bit representation, so that is what MrChips reasonably assumed. But if it had been, say, a six bit representation then the answer would have been a value of +8. The reason the number of bits must be fixed and known is that the sign bit is the left-most bit available in the representation.

The second fine point is that, in 2's comp, the pattern consisting of a 1 followed by all zeros is a troublesome one. All binary signed representations have at least one troublesome pattern. The reason is that the number of values in the range -N to +N is odd (N strictly positive values, N strictly negative values, and 0) while the number of patterns is even. Thus there will either be a value that has a pattern while its additive inverse doesn't, or there will be a value that has more than one pattern. In 2's comp, the additive inverse of the most negative value has no pattern. For the 4-bit example, the value -8 has the pattern 1000 while the value +8 can't be represented. But another way to look at it is to ask what the additive inverse of the pattern 1000 is using the algorithm of inverting all the bits and then adding one. You get 1000 back. This says that whatever value is represented by the pattern 1000, it is its own additive inverse. But there's only one value that is its own additive inverse, and that is zero, so 1000 must represent zero. Hence, the value zero has two representations in 2's comp. So which is it? The answer is that it is ambiguous, although most hardware is designed to treat this pattern as the most negative value (-8 in this case).
 

MrChips

Joined Oct 2, 2009
30,802
Another way at looking at this problem is the following:

4 bits can represent 16 unique values.

If one bit represents the sign then half the numbers are positive and the other half are negative. Hence there are 8 positive numbers (0 to 7) and 8 negative numbers (-1 to -8).

Thus we have to make the peculiar conclusion that zero is a positive number.

The value 8 is not represented in this 4-bit representation.

The beauty with 2's complement representation is that it is consistent with standard addition and subtraction arithmetic. When you add or subtract 1 from any number in the following series you move to the next number in sequence, wrapping around at the ends.

7
6
5
4
3
2
1
0
-1
-2
-3
-4
-5
-6
-7
-8
 

WBahn

Joined Mar 31, 2012
30,052
And it is this behavior that I'm hoping the OP will recognize as being the key to answering his question. The behavior is possibly best observed by ordering the patterns in straight binary order from largest to smallest and then writing the represented value next to them.

Pattern||Straight|2's comp
F||15|-1
E||14|-2
D||13|-3
C||12|-4
B||11|-5
A||10|-6
9||9|-7
8||8|-8
7||7|7
6||6|6
5||5|5
4||4|4
3||3|3
2||2|2
1||1|1
0||0|0

With straight binary, it is easy to tell if X>Y since the ordering of the values is the same as the alphabetical ordering of the patterns. But with 2's complement this same thing is true provided that both X and Y are the same sign.
 
Top