long 参数的位计算溢出,就好像它是 int
Bit calculation on long argument overflows as if it were int
谁能解释一下为什么下面的计算会溢出?
938372878L | (1 << 31) //results to -1209110770
它的行为就像数字是整数一样。
1
是一个 int
文字,这就是 (1 << 31)
溢出的原因。您应该将其更改为 1L
以将其视为 long
:
938372878L | (1L << 31)
这导致
3085856526
谁能解释一下为什么下面的计算会溢出?
938372878L | (1 << 31) //results to -1209110770
它的行为就像数字是整数一样。
1
是一个 int
文字,这就是 (1 << 31)
溢出的原因。您应该将其更改为 1L
以将其视为 long
:
938372878L | (1L << 31)
这导致
3085856526