Bitwise shift operation in C on uint64_t variable

前端 未结 4 2049
半阙折子戏
半阙折子戏 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 03:01

    Other posters have shown why it does this. But to get the expected results:

    uint64_t x, y; 
    x = ~(0xFULL<<24); 
    y = ~(0xFFULL<<24);
    

    Or you can do this (I don't know if this is is any slower than the above though):

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

    Then:

    x = 0xfffffffff0ffffff
    y = 0xffffffff00ffffff
    

提交回复
热议问题