Home > database >  What is the working mechanism of inv() in kotlin?
What is the working mechanism of inv() in kotlin?

Time:11-02

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()))

CodePudding user response:

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!

  • Related