&是什么LT;&LT; =运算符在Java中意味着什么? [英] What does <<= operator mean in Java?
问题描述
能否请您解释这一code段<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#HashMap\"相对=nofollow> HashMap的构造专行
&能力LT;&LT; = 1:
块引用>//找到2 - 电源=参数:initialCapacity
198 INT容量= 1;
而199(容量&LT;参数:initialCapacity)
200容量和LT;&LT; = 1;
解决方案这是等同于
容量=能力和LT;&LT; 1;
结果
该操作位移量的位一个位置的左边,这相当于乘以2您发布的具体code发现2的最小功率比
大参数:initialCapacity
。所以,如果
参数:initialCapacity
27,例如,容量
将在循环之后是32(2 ^ 5)Can you please explain this code snippet from HashMap constructor specifically the line
capacity <<= 1:
// Find a power of 2 >= initialCapacity 198 int capacity = 1; 199 while (capacity < initialCapacity) 200 capacity <<= 1;
解决方案It is equivalent to
capacity = capacity << 1;
.
That operation shifts capacity's bits one position to the left, which is equivalent to multiplying by 2.The specific code you posted finds the smallest power of 2 which is larger than
initialCapacity
.So if
initialCapacity
is 27, for example,capacity
will be 32 (2^5) after the loop.这篇关于&是什么LT;&LT; =运算符在Java中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!