I read that one can use “char” for small integers. However, when I try,
unsigned char A = 4;
std::cout << A << std::endl;
it gives a character, not 4.
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.
What you are experiencing are the effects of operator overloading. The
<<operator assumes that you most likely want to print a character when you hand it a variable of typechar, therefore it behaves differently than you would expect it for integral variables.As suggested by Vivek Goel you can force the compiler to select the overload you actually want:
Addendum: Unless you are working on an environment with severely constrained resources (esp. tiny memory), you are optimising on the wrong end. Operations on
unsigned intare typically faster than onunsigned char, because your processor cannot fetch single bytes but has to get at least a multiple of 4 and mask out the other 3 bytes.