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,466
  • 11
  • 55
  • 81

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
  • 374,785
  • 51
  • 663
  • 734