0

Possible Duplicate:
Difference between >>> and >>

Could someone explain what the >>> operator does in in Java. I encountered it in this question, Hash method in HashMap. From its usage in the question it appears to be a shift operator - what's the difference between >>> and >>?

Community
  • 1
  • 1
auser
  • 6,307
  • 13
  • 41
  • 63

3 Answers3

4

From Java tutorial:

The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

Smi
  • 13,850
  • 9
  • 56
  • 64
  • 1
    +1 Java needs this operator because `int` and `long` types are signed. It doesn't need an unsigned left shift because that would do the same thing. – Peter Lawrey Aug 29 '12 at 10:56
2

You can see the difference with a simple program:

public static void main(String[] args) throws InterruptedException, IOException {
    int i = -1;
    int j = i >> 1;
    int k = i >>> 1;
    System.out.println("i = " + i + "\t\t<=> " + Integer.toBinaryString(i));
    System.out.println("j = " + j + "\t\t<=> " + Integer.toBinaryString(j));
    System.out.println("k = " + k + "\t<=> " + Integer.toBinaryString(k));
}

output:

i = -1          <=> 11111111111111111111111111111111  
j = -1          <=> 11111111111111111111111111111111  
k = 2147483647  <=> 1111111111111111111111111111111
assylias
  • 321,522
  • 82
  • 660
  • 783
0

the signed right shift operator ">>" shifts a bit pattern to the right.The unsigned right shift operator ">>>" shifts a zero into the leftmost position,

more information

Follow this link

Hungry Blue Dev
  • 1,313
  • 16
  • 30
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37