0

CASE 1:

Let me try to represent -7 in 2's complement in 4 bits:-
7 in binary : 0111
7 in 1's complement:1000
7 in 2's complement:1001
So converting my 2's number to decimal: -1x2^3+0+0+1x2^0
=> -8+1=-7 its correct !!!

CASE 2:

Let me try to represent 7 in 2's complement in 4 bits:-
-7 in binary : 1111
7 in 1's complement:1000
7 in 2's complement:1001
So converting my 2's number to decimal: -1x2^3+0+0+1x2^0
=> -8+1=-7
its still -7 and i am wrong!!!!

vekerdyb
  • 1,213
  • 12
  • 26
DrDoggo
  • 149
  • 10
  • Possible duplicate of [What is “2's Complement”?](https://stackoverflow.com/questions/1049722/what-is-2s-complement) – vekerdyb Jul 13 '19 at 15:41

1 Answers1

1

You have slightly mixed up the concepts -

MSB(Most significant bit) represents sign of a number. Where 0 stands for positive and 1 stands for negative. Also, MSB do not participate in number conversions (if its representing sign).

Hence, -7 in binary is 1111 and not 0111.

Now, to do addition, subtraction, division, multiplication on binary numbers we can either use signed numbers with MSB and define a new est of rules to get the correct result.

OR

Use complements method to do it.

To calculate two's complement of a number

  1. Invert all the bits (0111) -> (1000)
  2. Add one to the result (1000 + 1) -> (1001)
    +7 -> 0111

(+) -7 -> 1001 (2s complement)

......................

     0 -> 10000 (ignoring the carry over MSB we will get zero which is the correct answer)
Community
  • 1
  • 1
Gaurav Mathur
  • 804
  • 5
  • 14
  • Aren't you confusing two's complement representation with sign-magnitude representation? They both exist, although I don't think there is any extant computer to use the sign-magnitude representation for integers in hardware. -7 in 4-bits two's complement is most definitely not 1111. That would be -1, quite obviously. (Hint: start with 0000 and subtract 1.) (Second hint: -1 is all ones in two's complement regardless of the number of bits.) – AlexP Jul 13 '19 at 17:11
  • @AlexP yeah you are right and that's what I have written? I don't understand the confusion. 2s complement of -1 is 1111 that's correct and that of -7 is 1001. I never said 2s complement of -7 is 1111. – Gaurav Mathur Jul 13 '19 at 19:22