简体   繁体   中英

Double Greater Than Sign (>>) in Java?

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.

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) .

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

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. 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:

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.

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

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.

// 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. (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.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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