#include <iostream>
int main()
{
signed int a = 5;
unsigned char b = -5;
unsigned int c = a > b;
std::cout << c << std::endl;
}
This code prints 0
.
Can anyone please explain what is happening here? I am guessing that compiler converts a
and b
to same type(unsigend int
maybe) and compares them.
Let's see how the computer stores the value b:
5
is00000101
, so-5
will be11111011
, so, when you convert it tounsigned char
, it will became some positive number with value11111011
in binary, which is larger than00000101
.So, that's why
a = 00000101
is smaller thanb
(0 means false).