简体   繁体   English

java中的“>>>”是什么意思?

[英]What does “>>>” in java mean?

im trying to translate this code to python, but im having a hard time doing so, don't worry about the index values and variable names, I just want to know what the ">>>" part does exactly, perhaps a python equivalent, but an explanation would be great: 我试图将此代码转换为python,但我很难这样做,不要担心索引值和变量名称,我只是想知道“>>>”部分究竟做什么,也许是python等价物,但解释会很棒:

target[0] = (char)(source[sourceIndex] >>> 2);
target[1] = (char)((source[sourceIndex] & 3) << 4 | source[sourceIndex + 1] >>> 4);
target[2] = (char)((source[sourceIndex + 1] & 0xf) << 2 | source[sourceIndex + 2] >>> 6);
target[3] = (char)(source[sourceIndex + 2] & 0x3f);

Any help would be appreciated 任何帮助,将不胜感激

The "<<<" and ">>" are bit shift operators. “<<<”和“>>”是位移操作符。 Specifically, 特别,

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. 带符号的左移运算符“<<”将位模式向左移位,带符号的右移运算符“>>”将位模式向右移位。 The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. 位模式由左侧操作数给出,位置数由右侧操作数移位。 The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension. 无符号右移运算符“>>>”将零移动到最左侧位置,而“>>”之后的最左侧位置取决于符号扩展。

—— from The Java™ Tutorials - Bitwise and Bit Shift Operators - 来自The Java™Tutorials - 按位和位移算子

It's an "unsigned right shift". 这是一个“未签名的右移”。

So, if your number ( x ) is 11110000 (in binary). 所以,如果你的数字( x )是11110000 (二进制)。

x >>> 1 will be 01111000 (in binary). x >>> 1将为01111000 (二进制)。

This is opposed to x >> 1 which will result in 11111000 (in binary). 这与x >> 1相反,这将导致11111000 (二进制)。

The >> tries to preserve the "sign bit" but the >>> does not. >>尝试保留“符号位”,但>>>不会。

Note: I've assumed a 8-bit integer (or a byte in Java). 注意:我假设一个8位整数(或Java中的一个byte )。 The same thing holds for 2-byte and 4-byte integers. 对于2字节和4字节整数,同样适用。

Thats the unsigned right shift operator. 那是无符号右移运算符。 It's a bitwise operator that shifts a zero into the leftmost bit of your operand. 它是一个按位运算符,将零移动到操作数的最左边位。 Here - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html . 这里 - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

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

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