As you supposed, 1 is a plain signed int
(which probably on your platform is 32 bit wide in 2's complement arithmetic), and so is 43, so by any chance 1<<43
results in an overflow: in facts, if both arguments are of type int
operator rules dictate that the result will be an int
as well.
Still, in C signed integer overflow is undefined behavior, so in line of principle anything could happen. In your case, probably the compiler emitted code to perform that shift in a 64 bit register, so by luck it appears to work; to get a guaranteed-correct result you should use the second form you wrote, or, in alternative, specify 1
as an unsigned long long
literal using the ull
suffix (unsigned long long
is guaranteed to be at least 64 bit).
var ^ ( 1ULL << 43 )
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…