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.
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 (
In order to convert this to negative, we need to take the one’s complement… or just inverse everything. Now we have
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
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.
- Binary Numbers (woodpeckersclass.wordpress.com)
- Binary Heaps are cool, seriously. (alikhuram.wordpress.com)
- Brothers in Binary (futilitycloset.com)
- Data Presentation (billatnapier.wordpress.com)
- Binary Search Tree Sum Function returning memory address (stackoverflow.com)
- Simple Binary to Decimal Conversion in C-Sharp (codingticks.wordpress.com)
- Resources For Teaching Binary Numbers (acthompson.net)