2's compliment

• Sep 12th 2011, 10:29 PM
Jskid
2's compliment
If a number is negative are all the leading bits 1?
For example I tried converting -26 with 2's compliment and got 11100110 but my calculator converted as 1111100110
• Sep 12th 2011, 11:20 PM
Re: 2's compliment
not all the leading bits. Only the MSB(most significant bit) has to be 1. you've used 8-bit number system. The calculator must be using 10-bit number systems. so in 8-bit system -26 is represented as 11100110 and in 10-bit system it is shown as 1111100110
• Sep 13th 2011, 09:38 AM
Jskid
Re: 2's compliment
So to convert from a lower bit to a higher bit, the leading bits of the higher bit are the same as the leading bit from the lower bit?
In the example to convert to 10 bit we added two 1 to the beginning because it was negative? if it had been positive we would have added two 0s to the beginning?
• Sep 13th 2011, 02:46 PM
emakarov
Re: 2's compliment
Quote:

Originally Posted by Jskid
So to convert from a lower bit to a higher bit, the leading bits of the higher bit are the same as the leading bit from the lower bit?
In the example to convert to 10 bit we added two 1 to the beginning because it was negative? if it had been positive we would have added two 0s to the beginning?

Yes. If the number is positive, then it is clear it should be padded with zeros on the left.

Suppose that we have an n-bit negative number, i.e.,

$-2^{n-1}+a$

for some $0\le a<2^{n-1}$, and suppose we add k bits. Then if we pad them with ones, the resulting number is

$-2^{n+k-1}+(2^{n+k-2}+\dots+2^{n-1})+a$.

Now,

$2^{n+k-2}+\dots+2^{n-1} = 2^{n-1}(2^{k-1}+\dots+1)=2^{n-1}(2^k-1)=2^{n+k-1}-2^{n-1}$,

so the numbers are the same.