简体   繁体   English

Java <<运算符(在这段代码中)

[英]Java << operator (In this piece of code)

So I was reading the example source code from this Android page on ViewGroups, and I came across these lines: 所以我从ViewGroups的这个 Android页面上阅读了示例源代码,并且遇到了以下几行:

// Report our final dimensions.
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
        resolveSizeAndState(maxHeight, heightMeasureSpec,
                childState << MEASURED_HEIGHT_STATE_SHIFT));

So I hope to learn: 所以我希望学习:

  1. What does the << operator exactly do in Java? <<运算符在Java中到底做什么?

  2. What is happening in last line of the aforementioned snippet? 在上述摘要的最后一行中发生了什么?

Thanks. 谢谢。

It isn't an operand, it is an operator. 它不是操作数,而是运算符。 A bit-wise shift operator , to be exact. 确切地说,是按位移位运算符

Given x and y as operands, x << y shifts the bits of the x value y bits to the left. 给定xy作为操作数, x << yxy位的位向左移动。 It is basically the same as multiplying by 2 to the power y . 它基本上等于乘以2的幂y

<< is one of bit shifting operator. <<是移位运算符之一。

It is quite offen we use higher 16 bits of one single 32 bits int for one thing and lower 16 bits of another thing. 我们很可能在一件事中使用一个32位int的高16位,而在另一件事中使用16位的低位。

childState << MEASURED_HEIGHT_STATE_SHIFT means childState is passing a height (in its lower 16 bits) which is expecting to be in higher 16 bits of the int passing to resolveSizeAndState(). childState << MEASURED_HEIGHT_STATE_SHIFT表示childState正在传递一个高度(以其低16位为单位),该高度期望传递给resolveSizeAndState()的int的高16位。

This is Bitwise and Bit Shift Operators. 这是按位和移位运算符。 More info can be found at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html 可以在http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html中找到更多信息。

For Ex: 例如:

1111 1110 << 2 
1111 1000  // Have added 0 from right

0001 1111 >> 3 
0000 0011 // Will preserve MSB and shift it right

If you don't want the first bit to be preserved, you use (in Java, Scala, C++, C afaik, and maybe more) a triple-sign-operator: 如果您不希望保留第一位,请使用(在Java,Scala,C ++,C afaik等中,甚至更多)三元符号运算符:

1100 1100 >>> 1
0110 0110 

"<<" is a bit operator. “ <<”是位运算符。

I quote the explaination from tutorial for Java 我引用了Java 教程中的解释

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. 有符号的左移位运算符“ <<”将位模式向左移位,而有符号的右移位运算符“ >>”将位模式向右移位。

And also I quote an example from here 我也从这里引用一个例子

int a = 60;     /* 60 = 0011 1100 */
int c = 0;
c = a << 2;     /* 240 = 1111 0000 */

so you can see a << n is almost a*(2^n) 因此您可以看到<< << 几乎是a *(2 ^ n)

  1. What does the << operator exactly do in Java? <<运算符在Java中到底做什么? It's moving bits to the left. 它向左移动位。 As mentioned in previous answers: 如先前的答案所述:

    1111 1110 << 2 1111 1000 // Have added 0 from right 1111 1110 << 2 1111 1000 //从右边添加了0

What is happening in last line of the aforementioned snippet? 在上述摘要的最后一行中发生了什么? Code is changing state. 代码正在更改状态。 It's sometimes used to represent states. 有时用来表示状态。 eg 例如

state1 = 1 << 0;
state2 = 1 << 1; 
state3 = 1 << 2;

so you have 3 unique states. 所以你有3个独特的状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM