-2

I have a question about the method of two's compliment: Which of these two methods are correct, and how can I disprove the incorrect one?

Starting example task: Convert the negative denary number -20 to two's compliment negative binary number (which is held in 8-bit binary).

Method 1:

  1. Find the positive binary value of the number,
  2. flip the bits of that binary value
  3. add one to the flipped number

Example: for -20.. 20 in binary is 00010100, those bits flipped are 11101011, then add 1 to the flipped bits to get 11101100. So, 11101100 is -20 in two's complement.

Method 2:

  1. minus one from the positive denary value
  2. Find the positive binary number of that denary number
  3. Flip the bits of that binary number

Example: for -20.. 20 is the positive denary number so 20-1 = 19. 19 in binary is 00010011. Flip the bits: 11101100. 11101100 is -20 in two's complement.

  • I'm voting to close this question as off-topic because it is not about practical programming but rather belongs on [Computer Science Stack Exchange](https://cs.stackexchange.com/) once the questioner has added more of his own work and explained just where he is stuck. – Rory Daulton Jan 15 '19 at 18:53

1 Answers1

0

Both methods are valid.

Given an n bits number A=an-1,an-2...a0, the C2 is the number such that A+C2(A)=2^n

If /A is the bit complement of A, it is easy to prove that A+/A=11..11=2^n-1 => C2=/A+1 which proves first method.

Second method states that C2(A)=/(A-1). If we compute /(A-1) + (A-1)=11...11=2^n-1, we can see that /(A-1)+A=2^n which proves second method.

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31