Two’s Complement

Some ways to cope with the 2’s complement.

Just a brief post regarding two tricks I’m aware of that make computing/converting a signed integer to binary easier. If you know any more please share.

The first comes from the Wikipedia article [link]:

A shortcut to convert a binary number into its two’s complement is to start at the right, and copy all the zeros (working from right to left) until the first 1 is reached; then copy that 1, and flip all the remaining bits.

This is a fairly straightforward trick. In fact, with a little work you can do the same thing when working in hexidecimal: again working from the right if the hex digits are zero copy them down, and then the first non-zero digit will be flipped but with an offset. It ties in with my second trick so I’ll lay that on you first.

It’s essential for two’s complement in hex to know the rot-8 table for the symbols. Yes, that old trick you may (or may not) have used for rot-13 or rot-n works like a charm. For rot-13 (basic letter replacement “cipher”) you end up with the following table:

a b c d e f g h i j k l m
n o p q r s t u v w x y z

Basically this allows you to easily see what any character/letter corresponds to for the replacement. Slightly different for hex-inversion:

0 1 2 3 4 5 6 7
F E D C B A 9 8

If you’ve got the number deadbeef and want the inversion: 21524110. Easy.

Now if you want to use the faster conversion trick for two’s complement I mentioned all you do is drop the 0 off the table and realize that 8 stays 8:

1 2 3 4 5 6 7 8
F E D C B A 9 8

And lickity-split you’re there. Hoorah.

Why this works: an example.

Let’s take the number -1984.

1984 = 7C0 h = 0111 1100 0000 b

Let’s start with the hex. Normally you’d take 7C0 and invert it all getting 83F. The difference is only adding one and seems trivial here. It’s worth it when you are working with 32 bit numbers with many zeros on the low order (rightmost) digits.

840, by the way. And now for the binary which will make it clearer why this shortcut is useful, at least for humans.

As stated, 0111 1100 0000; so walk right until you get to a 1: 100 0000 and now flip the rest: 1000 0; put it together: 1000 0100 0000. Compare that with taking 1000 0011 1111 and adding 1. It’s not that it’s that hard to add, it just adds more work though. carry, carry, carry, carry carry, carry, carry. Save yourself the trouble.

The fun things you learn when you delve into the inner workings of the computer.



Bonus, I’ll throw in another similar trick I learned back in grade school:


Those are your 9s multiplications right there.

These little tricks ultimately serve a single purpose– to ease the learning experience. You come to memorize these things after enough use; having a quick guide only makes it quicker. After you’ve looked at a table 20 times you probably won’t look again, and if you remember the tricks and you’re out of practice it’ll help you to spontaneously recover the learned behavior.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.