What is the effect of the following?
unsigned int x = -1; x >>= 2;
I’m not quite sure what the x gets set to as it’s a negative number and the type is unsigned?
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.
sets
xtoUINT_MAX(section 6.3.1.3, point 2)¹. Theint-1 is converted tounsigned intfor the initialistaion ofx. That conversion is done by adding (or subtracting, but not here)UINT_MAX +1to the value until it is in the range0 .. UINT_MAX, once here.Thus
x >>= 2;setsxtoUINT_MAX/4, since bitwise right shift of unsigned integers is thus specified in section 6.5.7, point 5:¹ “Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.”