This article is participating in the Java Theme Month – Java Debug Notes EventActive link

Life is a dream, but I’m invested in it. The world loved me first, I can’t help loving it. It’s So Good to Be Alive — Wang Zengqi

preface

We may often see code like the following, which is really confusing. Let’s take a look at these symbols and how they work. Remember to like it.

Read and write lock source code

Thread pool source code

There’s a little bit of binary here.

Let’s begin!

Describes the following operator calculations

~ & ^ |

Int is used here, but int is 32 bits (integer. SIZE, Long 64 bits). . Let’s take them down one by one!

With operation &

The result is 1 only if the middle bit of both operands is 1. Otherwise, the result is 0.

int a=129; //10000001 int b=127; //01111111, 0 before the positive digit is the complement. int c=-2; // negative 2 is 01, because it's a negative number. (Int is 32 bits.) 11111111111111111111111111111110, System. Out. Printf (" % n binary % s: % s ", a, Integer. ToBinaryString (a)); System. The out. Printf (" % n binary % s: % s ", b, Integer. ToBinaryString (b)); System. The out. Printf (" % n binary % s: % s ", c, Integer. ToBinaryString (c)); System.out.println("& and :"+(a&b)); System.out.println("& and :"+(a&c)); System.out.println("& and :"+(b&c));Copy the code

The results

Binary 129:10000001

Binary 127:1111111

Binary – 2:11111111111111111111111111111110

& and operation :1

& and operation :128

& and operation :126


So let’s do it in detail, and I won’t do it in detail in the next example, because it’s the same thing. Binary 129:10000001, here is to fill 32 bits. (int is 32 bits)

So 129 and 127

If both operands are 1, the result is 1; otherwise, the result is 0. Add 0 before positive numbers and 1 before negative numbers to fill 32 bits. The following example will expand the calculation of negative numbers.

00000000000000000000000010000001

00000000000000000000000001111111

00000000000000000000000000000001 / / this is the result, the result is 1.

& 129-2

The complement of negative numbers is computed in a special way.

The sign bit is the highest bit, the first left. For example, 01111111, where the 0 is the highest digit.

(1) If the symbol bit of the complement is “0”, it means that it is a positive number, and its original code is the complement.

⑵ If the sign bit of the complement is “1”, indicating that it is a negative number, then finding the complement of the given complement is the required source code.

So let’s figure out the complement of minus 2.

2 binary is 00000000000000000000000000000010

The first step to take the 11111111111111111111111111111101

The second step is to add 1 to 11111111111111111111111111111110 / / – 2’s complement

00000000000000000000000010000001

11111111111111111111111111111110

00000000000000000000000010000000 / / this is as a result, the result is 128.

| or operator

If one of the two bits is 1, the result is 1, otherwise it is 0.

/ / or the operator with the symbol "|" two / / as long as there is a 1, then the result is 1, otherwise 0 int a = 129; //00000000000000000000000010000001 int b=128; //00000000000000000000000010000000 int c=-2; / / 11111111111111111111111111111110 negative complement reference System. On the way out. The println (" | or operation: "+ (a | b)); System. The out. Println (" | or operation: "+ (a | c)); System. The out. Println (" | or operation: "+ (b | c));Copy the code

The results of

Note: The high level is 1, or negative. I want to keep the high end of the result, take the other one, add one. Get the result.

129 | | or operation: or operation: 1 | or operation: - 2Copy the code

The ~ non-operator

Non-operators are represented by the symbol “~”, and their operation rules are as follows:

If the bits are 0, the result is 1, and if the bits are 1, the result is 0

int a=129; //10000001 int b=128; //10000000 int c=127; //01111111, 0 before is the complement bit. int d=2; //10 int e=-3; //11 system.out. println("~ not :"+~a); System.out.println("~ non :"+(~b)); System.out.println(" +(~c)); System.out.println("~ not :"+(~d)); System.out.println(" e:"+(~e));Copy the code

The results of

~ non-operation :-130 ~ non-operation :-129 ~ non-operation :-128 ~ non-operation :-3 ~ non-operation e:2Copy the code

~ non-detailed calculation examples

Note that this is calculated manually:

Binary. 129:00000000000000000000000010000001

Take the first step, the result: 11111111111111111111111101111110 / / this machine can directly output – 130, as follows

//0b stands for binary

System.out.println(“~129:”+0b11111111111111111111111101111110);

So the simple way to think about it is 129 plus 1 is 130, take the opposite, make it negative, minus 130.

So the simple way to think about it is negative 3 plus 1 is negative 2, negative, positive 2.

The results of 11111111111111111111111101111110, is how to convert – 130?

Do the math manually.

Step one, keep the high order sign, invert everything else,

To: 10000000000000000000000010000001

Add a 10000000000000000000000010000010

The high level doesn’t participate in the calculation, so you get 130, and since the high level is 1, it’s negative, so you get -130.

^ Xor operator

The xOR operator is represented by the symbol “^” and operates as follows:

In the bits of two operands, the result is 0 if they are the same, and 1 if they are different

Int a=1; int a=1; //10000001 int b=2; //10, remember to complete the bit. int c=-3; / / / / 11111111111111111111111111111101 / / complement System. Out. The printf (Integer) toBinaryString (c)); System. The out. Printf (" ^ ^ exclusive or operation % s % s: % s % n ", a, b, a ^ b); System. The out. Printf (" ^ ^ exclusive or operation % s % s: % s % n ", b, c, b ^ c); System. The out. Printf (" ^ ^ exclusive or operation % s % s: % s % n ", a, c, a ^ c);Copy the code

The results of

^ Xor operation 129 ^ 2:131 ^ Xor operation 2 ^ -3: -1 ^ Xor operation 129 ^ -3: -132Copy the code

Operators <<, >>, >>>

<< Left-shift operator moves the object to the left of the operator to the left of the specified number of bits to the right of the operator x<<3
> > The “signed” right-shift operator moves the object to the left of the operator to the right by the number of digits specified on the right of the operator. Use the sign extension mechanism, that is, if the value is positive, 0 is added at the high level, and if the value is negative, 1. X >>3 is added at the high level
> > > The “unsigned” right-shift operator moves the object to the left of the operator to the right by the number of digits specified on the right of the operator. 0 expansion mechanism is adopted, that is, no matter the value is positive or negative, 0 is added at the high level.
/ / system.out. println("<<:"+(2<<3)); / / system.out. println("<<:"+(2<<3)); /** */ system.out. println(">>:"+(16>>2)); System.out.println(">>:"+(-16>>2)); System.out.println(">>>:"+(-8>>>1)); System.out.println(">>>:"+(-0b00001000)); System.out.println(">>>:"+(-0b00000100));Copy the code

The results of

<<:16 >>:4 >>:-4 >>>:2147483644 >>>:-8 >>> >:-4Copy the code

conclusion

The next article, we come to the actual combat, pick thread pool source code, incidentally review the old.

Deep state value analysis of JAVA thread pool source code

My knowledge is limited, if there is any wrong description, I would like to correct.

Look at thisLooks like you owe me a thumbs-up. Thank you.

Your praise is like the warm sun in winter, warming the heart.