I am surprised by C++\'s behavior when applying bit-wise not to an unsigned char.
Take the binary value 01010101b, which is 0x55, or
You can kind of "truncate" the leading 1's by assigning the result of ~0x55 to an unsigned char:
#include
int main()
{
unsigned char input = 0x55;
unsigned char output = ~input;
std::cout << "input: " << (int)input << " output: " << (int)output << std::endl;
return 0;
}
Result:
input: 85 output: 170