Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signed and unsigned char

Why are two char like signed char and unsigned char with the same value not equal?

char a = 0xfb; 
unsigned char b = 0xfb;
bool f;
f = (a == b); 
cout << f;

In the above code, the value of f is 0.
Why it's so when both a and b have the same value?

like image 412
T.g Avatar asked Dec 01 '22 10:12

T.g


2 Answers

There are no arithmetic operators that accept integers smaller than int. Hence, both char values get promoted to int first, see integral promotion for full details.

char is signed on your platform, so 0xfb gets promoted to int(-5), whereas unsigned char gets promoted to int(0x000000fb). These two integers do not compare equal.

On the other hand, the standard in [basic.fundamental] requires that all char types occupy the same amount of storage and have the same alignment requirements; that is, they have the same object representation and all bits of the object representation participate in the value representation. Hence, memcmp(&a, &b, 1) == 0 is true.

like image 79
Maxim Egorushkin Avatar answered Dec 04 '22 01:12

Maxim Egorushkin


The value of f and, in fact, the behaviour of the program, is implementation-defined.

In C++14 onwards1, for a signed char, and assuming that CHAR_MAX is 127, a will probably be -5. Formally speaking, if char is signed and the number does not fit into a char, then the conversion is implementation-defined or an implementation-defined signal is raised.

b is 251.

For the comparison a == b (and retaining the assumption that char is a narrower type than an int) both arguments are converted to int, with -5 and 251 therefore retained.

And that's false as the numbers are not equal.

Finally, note that on a platform where char, short, and int are all the same size, the result of your code would be true (and the == would be in unsigned types)! The moral of the story: don't mix your types.


1 C++14 dropped 1's complement and signed magnitude signed char.

like image 32
Bathsheba Avatar answered Dec 04 '22 02:12

Bathsheba