C++ - Bit-wise not of uchar produces int

后端 未结 3 1804
感情败类
感情败类 2021-01-05 09:54

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

3条回答
  •  失恋的感觉
    2021-01-05 10:34

    The answer about integral promotion is correct.

    You can get the desired results by casting and NOTting in the right order:

    assert(static_cast(0xAAu) == static_cast(~0x55u));
    

提交回复
热议问题