Hi,
I know it's old thread, but if you still don't know the answer then I think it's the type of character value, if you try inverting unsigned char 'A' (65) it will
give you 190.
It's still weird though, I can't get why when you type:
int main()
{
unsigned char sample = 'A';
cout << static_cast<int>(sample) << endl;
unsigned char sample2 = ~sample;
cout << static_cast<int>(sample2) << endl;
cout << static_cast<int>(~sample) << endl;
return 0;
}
you will get output:
65
190
-66
I don't get why when for static_cast function I use rvalue ~sample it still gives -66

. I think after checking: ~sample also would give -66 so I guess operator ~ implicitly converts sample to type signed char before reversing bits and then outputs as int.
Analyze the output of the below so it may make it more clear for you:
unsigned char sample = 'A';
cout << static_cast<int>(sample) << endl;
unsigned char sample2 = ~sample;
cout << static_cast<int>(sample2) << endl;
cout << static_cast<int>(~sample) << endl;
cout << sample << endl;
cout << ~sample << endl;
cout << sample2 << endl;
return 0;
Now I have also tried below lines and it worked so I guess you need to explicitly convert your value to unsigned char by any means:
cout << static_cast<int>(static_cast<unsigned char>(-66)) << endl;
cout << static_cast<int>(static_cast<unsigned char>(~sample)) << endl;
I hope this helps.