two’s complement

I thought I’d do a quick write up on how to convert positive number to negative in two’s complement. This topic because it’s really simple and some sources make it more complicated that it should be. As you know, two’s complement is by far the most widely used binary scheme used by computers today. Its used because it makes arithmetic between positive and negative values much more intuitive.

Method 1:

flip all the bits of the binary string and add 1.

For example:

1011 = (11 in decimal) becomes

0100 and then we add 1 so we get

0101

Easy enough. But there is one more easy method thats even quicker

Method 2:

flip all the bits after the first LSB(least significant bit) that is 1

For example:

1011 = (11 in decimal) becomes

0101 because we flipped all the bits after the first 1 which is the first LSB(least significant bit) in this case.

Padding:

I hope this method helped. Another side note is that when padding two’s complement numbers keep in mind that you pad negative numbers with 1s to the left and positive numbers with 0s to the right.

For example:

If we want to write 1011 (11 in decimal) using 8 digits we would write it as 00001011

and if we want to 0101 (-11 in decimal) using 8 digits we would write it as 11111011.

Adding:

Adding positive twos complement numbers is pretty straight forward.

For example:

10(2 in decimal) + 10(2 in decimal) = 100 (4 in decimal)

Adding twos complement numbers that involve a negative is straight forward as well.

1111(-1 in 2s complement) + 1111(-1 in 2s complement) = 1110 (-2 in 2s complement)

However, the only difference is that we drop the carry bit. There was a carry of 1 in this case but we dropped it.

Add a Comment

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