What does << = operator mean in Java?

Could you explain this piece of code from the HashMap constructor , namely the line

capacity <= 1:

// Find a power of 2 >= initialCapacity
198         int capacity = 1;
199         while (capacity < initialCapacity)
200             capacity <<= 1;
+5
source share
4 answers

It is equivalent capacity = capacity << 1;.
This operation shifts the power bits one position to the left, which is equivalent to multiplying by 2.

The code you define detects the least power 2, which is greater than initialCapacity.

So, if initialCapacityequal to 27, for example, there capacitywill be 32 (2 ^ 5) after the loop.

+11
source

, var += 1 var = var + 1, , (var <<= 1), var = var << 1, " var a 1 - var."

( ) capacity *= 2 ( 1 2).

+4

capacity = capacity << 1;

capacity (.. 00011011 00110110).

+3

, , "" 2, .

as originally, it is 1 ie2 ^ 0; operation (power <= 1) for the first time makes it 2 ^ 1, and then 2 ^ 2 and so on. Maybe you would like to see more on it at http://www.tutorialspoint.com/java/java_basic_operators.htm

0
source

All Articles