0

I recently gave an aptitude exam and encountered this piece of code:

public class MainClass{
    public static void main(String[] argv){
        int x = 0x80000000; 
        x = x >>> 31;
        System.out.println(x);
    }
}

At first I thought it must be some sort of error, but this gives a valid output of 1. With x >> 31, the output is -1, which is understandable, but what does the extra > do?

Krishh
  • 602
  • 1
  • 8
  • 25

2 Answers2

1

It's called unsigned shift right which means that it always fills in zeros regardless of the sign of the original int.

Mark H
  • 29
  • 2
1

Have a look here: https://www.javatpoint.com/operators-in-java. The >>> operator is same as >> with an exception that it handles signed integers differently.

hello512
  • 379
  • 4
  • 16