Why does System.out.println(-1<<32) display -1 in Java? Is there any root cause? I hope somebody can explain!
Why does System.out.println(-1<<32) display -1 in Java? Is there any root cause? I hope
Share
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.
In Java (and JavaScript),
a << bautomatically wrapsbmodulo the bit length of the numeric type. You’re doing it with anint, which is 32 bits, and 32 % 32 = 0, therefore, no change. You can always just check for abof more than (bit length – 1) and automatically set to zero instead of shifting.