System.out.print((-15) & (-1 >> 1));
Why does the above code return -15?
Shouldn't it set the MSB to 0 as the bitmask is (-1 >> 1) which is 01111111
I was expecting a positive integer but it returns -15. The logic is unclear. Kindly help
According to java docs:
The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
So >> keeps the sign and you probably nedd an >>>