Bitwise shift operation in C on uint64_t variable

前端 未结 4 2010
半阙折子戏
半阙折子戏 2021-01-20 02:21

I have the following sample code:

uint64_t x, y;
x = ~(0xF<<24);
y = ~(0xFF<<24);

The result would be:

x=0xffff         


        
4条回答
  •  暖寄归人
    2021-01-20 02:52

    Because 0x0f << 24 is a positive number when viewed as an int, it's sign-extended to a positive number, i.e. to 0x00000000_0f000000 (the underscore is just for readability, C does not support this syntax). This is then inverted into what you're seeing.

    0xff << 24 on the other hand is negative, so it's sign-extended differently.

提交回复
热议问题