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

Time：2020-10-31

### introduce

First, let’s look at a program and print it separately`4`and`-4`The 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);``

### understand

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 right`4`The process is as follows:

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

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

1. First look at the complement`1 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 code`1 010`The original code is obtained by dividing the symbol bit and taking the reverse bit`1 101`This is not`-5`Yes

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

1. Binary representation (complement) of 64:`1 100`
2. The computer reverses it bit by bit`0 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, complement`0 011`The 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 to`3`.

``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（ http://www.gongsir.club )

## Common regular expressions are more practical

Regular expression matching Chinese characters: [\ u4e00 – \ u9fa5]Comment: matching Chinese is really a headache. It’s easy to do with this expression Matching double byte characters (including Chinese characters): [^ \ X00 – \ XFF]Comment: can be used to calculate the length of a string (a double byte character length meter 2, ASCII character […]