The definition of the shift operators means that only the 5 least significant bits are used for 32-bit numbers and only the first 6 bits for 64-bit; meaning:
1 << 5
is identical to
1 << 37
(both are 32
)
By making it:
MsgrVersion9 = 1L << 32
you make it a 64-bit number, which is why @leppie’s fix works; otherwise the <<
is considered first (and note that 1<<32
is identical to 1<<0
, i.e. 1
), and then the resulting 1
is converted to a long
; so it is still 1
.
From ยง14.8 in the ECMA spec:
For the predefined operators, the number of bits to shift is computed as follows:
- When the type of x is
int
oruint
, the shift count is given by the low-order five bits of count. In other words, the shift count is computed fromcount & 0x1F
.- When the type of x is
long
orulong
, the shift count is given by the low-order six bits of count. In other words, the shift count is computed fromcount & 0x3F
.If the resulting shift count is zero, the shift operators simply return the value of x.
Shift operations never cause overflows and produce the same results in checked and unchecked context