int a = 1 << 32;
int b = 1 << 31 << 1;
Why does a == 1? b is 0 as I expected.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
All shifts are done mod 32 for ints and mod 64 for longs.
From section 15.19 of the spec:
As for why the language was designed that way – I don’t know, but C# has the same design decision. Here’s what the annotated ECMA C# spec says:
(There’s then a list of some examples.)
This seems an entirely reasonable explanation to me. Consistency is definitely important, and if it would be impossible to implement different consistent behaviour in a performant way on some systems, I think this is a reasonable solution.