3

There is an interesting fact:

The 2's complement of a number N is equivalent to 1's complement of the number N minus 1.
i.e.

2's(N) = 1's(N-1)

The below result is obvious.

2's(N) = 1's(N) + 1

How the first result can be proved with the help of second one?

Green goblin
  • 9,898
  • 13
  • 71
  • 100

1 Answers1

1

Both the 2's complement and the 1's complement map one region of negative numbers onto a region of positive numbers in a manner that is easy for a CPU to deal with.

In the case of 8-bit numbers the 1's complement maps -127..-0 to 128..255, on the other hand the 2's complement maps -128..-1 to 128..255.

You can perform the 1's complement and the 2's complement again, so repeated application of 1's complement and 2's complement just gets you back to the same place. (So, we do not need to worry about concerning ourselves with positive numbers.)

If you look at the ranges provided each number from 128 to 255 is replaced by a single number, and this number has a difference of 1 between the 2's complement and the 1's complement.

Stated mathematically (for 8-bit numbers):

2's(N) = 1's(N) + 1
2's(N) = N ^ 0xFF + 1
2's(N) = N ^ 0xFF + 0xFE ^ 0xFF
2's(N) = (N + 0xFE) ^ 0xFF
2's(N) = (N - 1) ^ 0xFF
2's(N) = 1's(N-1)

Justification for each step:

Step 1: Given
Step 2: Definition of 1's complement
Step 3: Identity
Step 4: Distribution
Step 5: Identity (within 1's complement system)
Step 6: Definition of 1's complement
OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
  • Step 5 is not correct. (N + 0xFE) gives (N - 2). See here: http://ideone.com/ovQvd – Green goblin Aug 25 '12 at 16:29
  • It gives N-2 in the 2's complement system. When you're working with the 1's complement system you're required to add an additional 1 when dealing with overflows (to account for -0). I assumed you knew that so I didn't mention it. – OmnipotentEntity Aug 25 '12 at 16:33