The difference between Java >> and >>> and >>>=
Maybe we all know the difference between >> and >>> in Java, and I'll say it here too.,>> and >>> are all right-shifted, except that >>> is the unsigned right shift, which is the high-level complement 0 after the right shift. For positive numbers, there is no difference between the two, but for negative numbers, the,>> is negative after the right shift, and after the >>> right shift becomes a positive number, you can look at the example:
1 Public classShifttest {2 Public Static voidMain (string[] args) {3 intn = 15;4 System.out.println (java.lang.Integer.toBinaryString (n));5SYSTEM.OUT.PRINTLN (n >> 2);6SYSTEM.OUT.PRINTLN (n >>> 2);7 8 intm =-15;9 System.out.println (Java.lang.Integer.toBinaryString (m));TenSystem.out.println (M >> 2); OneSystem.out.println (M >>> 2); A } - } Output Results1111 3 3) 11111111111111111111111111110001-4 1073741820
This example and the results we must also be able to understand, the only thing to note is that the computer is in the form of a complement of the storage, you can go to see the relevant information. Then let's talk about the difference between >>> and >>>= (because there is a classmate turned to see threadlocal source code, found the Cleansomeslots method has >>>= this operator, I have not seen this operation before, only know that >>> is unsigned right shift, so we discussed it, I then wrote a test code, as follows:
1 Public classShifttest {2 Public Static voidMain (string[] args) {3 intn =-4;4SYSTEM.OUT.PRINTLN (n >>>= 1);5SYSTEM.OUT.PRINTLN (n >>> 1);6 7 intj = 4;8System.out.println (J >>>= 1);9System.out.println (J >>> 1);Ten } One } A Output Results2147483646 1073741823 2 1
From this example, we may be more puzzled, why the 9th line will output 1, it should be 2 ah, I am also more puzzled, this time the classmate a bit, it dawned, in fact, J in the 8th Row was re-assigned to 2, and the 9th line to shift the right of course output 1. From this example we know that >>>= is similar to the + = operation, after the right shift made an assignment.
Summarize
In fact, the shift operation in our usual use is not much, but in the reading JDK source code often appear, such as concurrent bag threadpoolexcutor, Concurrenthashmap and so on. So it is necessary to master the shift operation as well as the original code, anti-code, and complement.
The difference between Java >>> and >>>=