if i have int temp=(1<<31)>>31. How come the temp becomes -1?
how do i get around this problem?
thanks
if i have int temp=(1<<31)>>31. How come the temp becomes -1? how do i
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.
Ints are signed by default, which usually means that the high bit is reserved to indicate whether the integer is negative or not. Look up Two’s complement for an explanation of how this works.
Here’s the upshot:
Notice how you can avoid this problem either by using an “unsigned” int (allowing the use of all 32 bits), or by going to a larger type which you don’t overflow.