0

why the answer is -5 ?

it should be 11 because all zeroes will be ones and all ones will be zeroes.

var v = 4 //0100 println("bit Inverse = "+(v.inv()))

small_boy
  • 5
  • 1
  • 1
    Does this answer your question? [What is “2's Complement”?](https://stackoverflow.com/questions/1049722/what-is-2s-complement) – Sam Nov 01 '22 at 13:54
  • @Sam i did 2's complement of 4 as unsigned number and result of 4 (binary = 0100) was 1100 (base10 = 12). When i did this for signed number, result was -4 in base 10 – small_boy Nov 01 '22 at 15:04

1 Answers1

1

In the two's complement system, the most-significant digit on the left represents a negative value - in this case, -8.

-8 4 2 1

So the largest positive value you can represent with 4 bits is 0111, or 7.

-8 4 2 1
 0 1 1 1 = 0 + 4 + 2 + 1

The largest negative value is 1000, or -8.

-8 4 2 1
 1 0 0 0  = -8 + 0 + 0 + 0

And the inverse of 0100 is 1011, which gives us

-8 4 2 1
 1 0 1 1  = -8 + 0 + 2 + 1

which is -5!

cactustictacs
  • 17,935
  • 2
  • 14
  • 25