• C

what actually happens when a character c is assigned a value of 164?


what happens when the folloowing:

char x = 64;
cout << (int)c ;

it prints -92
but the question is how ? how does it work internally? how does it arrive at -92?

thanks and regards
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Replace char to unsigned char.
Two's complement arithmetic.

For example (or use google):
x becomes 164.
In 2' s complement, it becomes:
10100100B=(-01011011B - 1) = -92

The way 2's complement works for signed values is, if the Most significant bit is 1, sign becomes negative(-), and get the absolute value by calculating  bitwise NOT of the current value and adding one to it (since there is nothing called -0).

As AlexFM says, using unsigned char should give you 164.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

What you need to note is that char is a one byte quantity. In 1 byte (8 bits) you can count from 0 through 255 if you consider it unsigned. But if you consider it signed, you can count from -128 to 127. So when you added 100 to x it became 0xA4, as linuxsub indicates. So this was now a 1 byte quantity with a decimal value of -92, that is, it had overflowed, because a 1 byte signed quantity cannot represent 164. *After* that, you cast it to an int. An int is also signed, and so the cast will "propogate" the sign bit during the conversion (in order to preserve the value; it was -92 before the cast, and this way it will also be -92 after the cast). Assuming a 4 byte int, you would get 0xFFFFFFA4.

You can solve this, as indicated, by using an unsigned char. That will allow you to represent 0 through 255.

More messily, you could also, of course force it to do what you want by masking off the top bits:


char can store values between  -128  to +127  and
164  = 10100100    in binary
The left most bit the sign bit is turned on meaning the number is negative
the rest of the number is   0100100  
now as the number  is negative so its stored as two's compilent  so its value becomes

 ~ 0100100] =>   1011011  +1  =>        1011100  
=>  -92
Did any of those answers help?

If so, it is now time to choose and answer and grade it.

If not, perhaps a clarifying question would help.
rags2kAuthor Commented:
All answers helped me but the first and most useful one tome was one by linuxsub..bu i am surprised that I am not allowed to accept/rate all answers on their merits...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.