0

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

Aditya
  • 1
  • 1

1 Answers1

0

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

gdomo
  • 1,650
  • 1
  • 9
  • 18