Search code examples
binarytwos-complementcomplementones-complement

Does the sign-bit change when using one's or two's complement?


I've been struggling with what seems like simple concepts, but I seem to keep combining and mixing up binary conversions.

If you are given a number in binary and apply 1's complement, you invert all bits. Making the 0's 1's and 1's 0's. For 2's complement you invert the bits like with 1's complement and add 1.

Does this mean you literally invert all bits? For some reason I keep thinking that all bits should be inverted except for the left most bit (the sign bit). Here are some examples.. Can someone please tell me if I'm doing this correctly?

1101001

1's comp: 0010110

2's comp: 0010111


01110101

1's comp: 10001010

2's comp: 10001011


011110110

1's comp: 100001001

2's comp: 100001011


Thanks.


Solution

  • Your concepts are totally correct:

    • one's complement = invert everything
    • two's complement = invert everything, including the left most bit, then add 1

    Your first two examples are correct, the last one is a little strange since you usually work with multiples of bytes (8 bits) and your example has 9 bits, but still, the same rules apply:

            (0000000)011110110
    ones -> (1111111)100001001
    twos -> (1111111)100001010  <-- I think you had a typo here, you stated the result would be 100001011
    

    Here's a nice summary article about the subject: http://academic.evergreen.edu/projects/biophysics/technotes/program/2s_comp.htm