简体   繁体   English

Java中的Double Greater Than Sign(>>)?

[英]Double Greater Than Sign (>>) in Java?

What does the >> sign mean in Java? >>符号在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. 我尝试在Google上搜索它,但没有找到任何有用的东西。

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. 在所有情况下(就像任何其他算术运算符一样),您应始终确保不会溢出数据类型。

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. 无符号右移运算符“>>>”将零移动到最左侧位置,而“>>”之后的最左侧位置取决于符号扩展。

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

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. 它的一般形式:value >> num这里,num指定右移值的位置数。 That is, the >> moves all of the bits in the specified value to the right the number of bit positions specified by num. 也就是说,>>将指定值中的所有位向右移动num指定的位位置数。 The following code fragment shifts the value 32 to the right by two positions, resulting in a being set to 8: 以下代码片段将值32向右移动两个位置,从而将值设置为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移位到右边两个位置,这导致两个低位比特丢失,再次导致设置为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. 每次将值向右移动时,它会将该值除以2,并丢弃任何余数。 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. 你可以利用这个来实现2的高性能整数除法。当然,你必须确保你没有将任何位移到右端。 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 例如, –8 >> 1–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. 有趣的是,如果你向右移动-1,结果总是保持-1,因为符号扩展会在高位中引入更多的符号。 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. 请注意,移位值通过与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

这是一个正确的位移。

I believe it's the bit shifting operator. 我相信这是一个有点转变的运营商。 As in moves all 1s and 0s one position right. 如同将所有1和0移动到一个位置右侧。 (I think you can imagine what << does... :) ) (我想你可以想象<<做什么...... :))

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 . 有关您的StackOverflow用户提供的有关位移的大量详细信息,请查看我很久以前发布的一个问题,这有助于我最终得到它: Absolute Beginner的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.) (发布在那里的人们非常友好地深入研究这个主题,我希望这也能帮到你。)

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

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