You don’t know the principle of bitwise inversion, do you



First, let’s look at a program and print it separately4and-4The reverse operation result of, code:

public static void main(String[] args) {    System.out.println(~4);    System.out.println(~(-4));}

Think about the results. If the results are – 4 and 4, please continue to look


Yes, the results are – 5 and 3 (not as simple as the opposite number). Here we tell a universal formula to calculate the inverse operation of an integer, such as X, then:

~x = -(x+1);


Although there is a formula, I believe you still like me, want to find out why such a result
First of all,~What is the definition of this computational symbol?
According to my usual understanding, when I use~In the bitwise inversion operation, the computer will reverse each bit of the binary expression corresponding to the operands. The value obtained after inversion is the result of bitwise inversion operation (this is no problem), but how to understand it, let’s analyze it in detail

Before that, let’s talk about the data representation in the computer
We usually learn binary representation in three forms: source code, inverse code and complement code;
However, the computer only knows the complement code, the original code and the inverse code are only artificially defined for people’s convenience, so all input and output of binary calculation in the computer are in the form of complement code. Understand this point, you can continue to look down

Or the above program, we are right4The process is as follows:

  1. Binary representation (complement) of 4:0 100
  2. The computer reverses it bit by bit1 011
  3. At this time, the computer will feed back the calculation results to us, and the answer is:-5

Maybe you still wonder why1011You get it-5Well, it shouldn’t be-3Is that right?
Back to what I just said, all binary outputs in a computer are in the form of complements, so1011It is the complement representation of a number. We need to convert it into binary that we can understand

  1. First look at the complement1 011, the highest bit is 1, which means that it is a negative number, so the sign bit remains unchanged, and then minus 1 to get the inverse code (complement code = inverse code + 1, vice versa)1 010;
  2. Will reverse code1 010The original code is obtained by dividing the symbol bit and taking the reverse bit1 101This is not-5Yes

In the same way, let’s look at it again-4Reversal:

  1. Binary representation (complement) of 64:1 100
  2. The computer reverses it bit by bit0 011
  3. At this time, the computer will feed back the calculation results to us, and the answer is:3

So why three again?
It’s very simple, complement0 011The highest bit is 0, indicating that it is a positive number. The original code and complement code of a positive number are the same, so they are directly equal to3.

public static void main(String[] args) {     System.out.println (~ 4); // complement: 0.100 // reverse: 1.011 // counter code: 1.010 // original code: 1.101 = - 5 System.out.println (~ (- 4)); // 1 100 // 1 011 // complement: 1 100 // reverse: 0 011 // original code: 0 011 = 3}

>Welcome to our attention( )