in below code when i run it i get y=-124 and z=4294967172
can you explain me?? (tested that if x<128 there isnt any problem)
char x=132;
signed y=x;
unsigned z=x;
cout<<y<<endl;
cout<<z<<endl;
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.
charis 8-bits, so when you writechar x = 132, you actually do this:signed intandunsigned intboth are 32-bits, and whenever you assign the value of a ‘smaller’ variable into a ‘larger’ one, the system uses sign extension, i.e. copies the sign bit to every bit to the left. So the value becomes:If you interpret it as a signed value, it’s -124, and as an unsigned value it’s 4294967172.
Moreover, if you define the
charasunsignedin the first line, you would always get 132, as sign extension is not done for unsigned values.