Java中的双大于号(>>)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1050989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Double Greater Than Sign (>>) in Java?
提问by Petey B
What does the >>
sign mean in Java? I had never seen it used before but came across it today. I tried searching for it on Google, but didn't find anything useful.
>>
Java中的符号是什么意思?我以前从未见过它使用过,但今天遇到了它。我尝试在 Google 上搜索它,但没有找到任何有用的信息。
采纳答案by Clint
This is the bit shift operator. Documentation
这是位移位运算符。 文档
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.
有符号左移运算符“<<”将位模式左移,有符号右移运算符“>>”将位模式右移。位模式由左边的操作数给出,右边的操作数给出要移位的位置数。无符号右移运算符“>>>”将零移到最左边的位置,而“>>”之后的最左边位置取决于符号扩展。
回答by mandaleeka
That is a right bit shift.
这是一个正确的位移。
回答by jacobangel
I believe it's the bit shifting operator. As in moves all 1s and 0s one position right. (I think you can imagine what << does... :) )
我相信这是位移运算符。正如在将所有 1 和 0 向右移动一个位置一样。(我想你可以想象<<做什么... :))
回答by Chris Klepeis
It shifts the bits...
它移动位...
heres some info on java operators
这里有一些关于java 操作符的信息
For example
例如
101 = 5
Shifting out the right "1"
10 = 2
Shifting the other way...
1010 = 10
回答by John Rudy
As others have noted, this is the right bit-shift. You'll see it in many of the so-called "C-style" languages.
正如其他人所指出的,这是正确的位移。您会在许多所谓的“C 风格”语言中看到它。
For massively detailed information about bit-shifting provided by your fellow StackOverflow users, check out a question I posted ages ago, which helped me finally get it: Absolute Beginner's Guide to Bit-Shifting. (The folks who posted there were kind enough to go into great depth on the subject, which I hope will help you as well.)
有关您的 StackOverflow 用户提供的有关位移位的大量详细信息,请查看我很久以前发布的一个问题,它帮助我最终获得了它:位移位绝对初学者指南。(在那里发帖的人非常友好地深入讨论了这个主题,我希望这也能帮助到你。)
回答by Yuval Adam
The >>
operator is the bitwise right shift operator.
该>>
操作是按位向右移位运算符。
Simple example:
简单的例子:
int i = 4;
System.out.println(i >> 1); // prints 2 - since shift right is equal to divide by 2
System.out.println(i << 1); // prints 8 - since shift left is equal to multiply by 2
Negative numbers behave the same:
负数的行为相同:
int i = -4;
System.out.println(i >> 1); // prints -2
System.out.println(i << 1); // prints -8
Generally speaking - i << k
is equivalent to i*(2^k)
, while i >> k
is equivalent to i/(2^k)
.
一般来说-i << k
相当于i*(2^k)
,而i >> k
相当于i/(2^k)
。
In all cases (just as with any other arithmetic operator), you should always make sure you do not overflow your data type.
在所有情况下(就像任何其他算术运算符一样),您应该始终确保不会溢出您的数据类型。
回答by star18bit
The Right Shift:
右移:
The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. Its general form :value >> num Here, num specifies the number of positions to right-shift the value in value. That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num. The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8:
右移运算符 >> 将值中的所有位右移指定的次数。它的一般形式:value >> num 这里,num 指定在 value 中右移值的位置数。也就是说,>> 将指定值中的所有位向右移动 num 指定的位位置数。以下代码片段将值 32 向右移动两个位置,从而将 a 设置为 8:
int a = 32;
a = a >> 2; // a now contains 8
When a value has bits that are “shifted off,” those bits are lost. For example, the next code fragment shifts the value 35 to the right two positions, which causes the two low-order bits to be lost, resulting again in a being set to 8.
当一个值的位被“移出”时,这些位就会丢失。例如,下一个代码片段将值 35 右移两个位置,这会导致两个低位丢失,从而再次将 a 设置为 8。
int a = 35;
a = a >> 2; // a still contains 8
Looking at the same operation in binary shows more clearly how this happens:
以二进制形式查看相同的操作可以更清楚地显示这是如何发生的:
00100011 35 >> 2
00001000 8
Each time you shift a value to the right, it divides that value by two—and discards any remainder. You can take advantage of this for high-performance integer division by 2. Of course, you must be sure that you are not shifting any bits off the right end.
When you are shifting right, the top (leftmost) bits exposed by the right shift are filled in with the previous contents of the top bit. This is called sign extension and serves to preserve the sign of negative numbers when you shift them right. For example, –8 >> 1
is –4
, which, in binary, is
每次向右移动一个值时,它都会将该值除以二,并丢弃任何余数。您可以利用这一点进行高性能整数除以 2。当然,您必须确保没有从右端移出任何位。当您右移时,右移暴露的顶部(最左侧)位将填充顶部位的先前内容。这称为符号扩展,用于在右移负数时保留负数的符号。例如,–8 >> 1
is –4
,在二进制中,是
11111000 –8 >>1
11111100 –4
It is interesting to note that if you shift –1 right, the result always remains –1, since sign extension keeps bringing in more ones in the high-order bits. Sometimes it is not desirable to sign-extend values when you are shifting them to the right. For example, the following program converts a byte value to its hexadecimal string representation. Notice that the shifted value is masked by ANDing it with 0x0f to discard any sign-extended bits so that the value can be used as an index into the array of hexadecimal characters.
有趣的是,如果将 –1 右移,结果始终保持为 –1,因为符号扩展会不断在高位中引入更多的 1。有时,当您将值向右移动时,不希望对其进行符号扩展。例如,以下程序将字节值转换为其十六进制字符串表示形式。请注意,移位值通过与 0x0f 进行 AND 运算来屏蔽,以丢弃任何符号扩展位,以便该值可以用作十六进制字符数组的索引。
// Masking sign extension.
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};?????
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}
Here is the output of this:
这是输出:
b = 0xf1