2

Could someone explain me why the following computation overflows?

938372878L | (1 << 31) //results to -1209110770

It behaves as if the number were integer.

Konstantin Milyutin
  • 11,946
  • 11
  • 59
  • 85

1 Answers1

4

1 is an int literal, which is why (1 << 31) overflows. You should change it to 1L for it to be treated as long :

938372878L | (1L << 31)

This results in

3085856526
Eran
  • 387,369
  • 54
  • 702
  • 768