# twos complement in 16bit?



## scott0999 (Jan 8, 2005)

I need to convert -10 and -70 to 16bit

now I read something about using "Two's complement" for negative numbers, but idk how to convert it to 16bit

found this program to get the 8bit version:

http://www.rsu.edu/faculty/PMacpherson/Programs/twos.html


----------



## burnthepc (Aug 12, 2007)

The wikipedia page is pretty good on two's complement: 
http://en.wikipedia.org/wiki/Two's_complement

Note how it goes:

0x0001 = 1 decimal
0x0000 = 0 decimal
0x1111 = -1 decimal

(as opposed to 0x1110 which seemed more logical to me - but it has to be like that for calculations to work)

Once you've got the grasp of what it does, doing 8bit 16bit or 32bit is just a bit of mental heavy lifting.


----------



## JimmySeal (Sep 25, 2007)

burnthepc said:


> (as opposed to 0x1110 which seemed more logical to me - but it has to be like that for calculations to work)


-1 would be 1110b in _one's_ complement binary and calculations are entirely possible in one's complement, but they are generally simpler in two's complement.

Converting a negative decimal number to two's complement is very simple in any number of bits:

(taking -27 in 8bit as an example)
1. Represent its absolute value in binary: 00011011
2. Invert all the bits: 11100100
3. Add 1: 11100101

To increase the number of bits, just append more 1s to the left side of the number.


----------



## iminvincible (Mar 24, 2009)

Basically all you do is inverse every number then add one, or their is another way where you go up to the first 1 in the 16 bit number, then inverse all the numbers after the 1st one.
EX/
00010--->11110


----------



## burnthepc (Aug 12, 2007)

JimmySeal said:


> (taking -27 in 8bit as an example)
> 1. Represent its absolute value in binary: 00011011
> 2. Add 1: 00011100
> 3. Invert all the bits: 11100011


That's a cool method. I did it the hard way for my architecture exams last year.

[hangs head in shame] Just spotted that the numbers I used should have been 0001b ect not 0x0001. My only defense is that it was late and I was tired


----------



## JimmySeal (Sep 25, 2007)

Thanks, but I need to hang my head in shame because I had steps 2 and 3 reversed. iminvincible's method is even simpler.


----------



## IMM (Feb 1, 2002)

removed - minor memory lapse


----------

