Link: Two’s Complement

Link: Two’s Complement

You wanna know what always used to break my brain back in school? Two’s complement. For those of you unversed in nerd geek, i’s the computer’s way of interpreting negative and positive binary numbers by using the left-most bit as the sign.

Examples:

Lets talk in nybbles here (4 bits instead of 8). In a perfect unsigned world, the number 15 is expressed as 1111 in binary. However, speaking in signs, this is -1.

Juicy. OK, lets work this over a bit. When dealing with signed numbers, our range, technically, gets cut in half, but now goes both positive and negative. Where unsigned, we had 0, …, 15, now we have -8, …, 7.

So lets take the number +4 ( 0100 ).

In order to convert this to negative, we need to take the one’s complement… or just inverse everything. Now we have 1011.

For two’s complement, take the one’s complement and add one. Seriously.

 1011
+   1
-----
 1100

That’s it. Why was this so hard for me back in school!?

Now, I know what you’re thinking: “Duh, obviously. The Sign bit is set to 1, and the bit in the 4’s place is set to 1, and all else is 0.”

Yes true. Honestly, dealing with nybbles is very simplistic. Lets do another.

 
+7      = 0111
Inverse = 1000

 1000
+   1
-----
 1001

Boom.

And that’s why it’s confusing. Check out the link, there’s a more in-dept explanation, more examples, and, hey, even a handy calculator. You’re welcome.

Advertisements

Published by

KeithM

Computer Scientist for Engility, and Indie Game Dev from New Jersey. Loves playing games, and loves making them, too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s