What does ~0 do?

前端 未结 4 1977
有刺的猬
有刺的猬 2020-12-11 08:13

Does ~0 mean its flipping 000000000 to 1111111111?

printf(\"Check: %i\", ~0);

The printf results to -1, which is why I am confused. <

4条回答
  •  自闭症患者
    2020-12-11 09:00

    ~0 == 0xFFFFFFFF

    where 0xFFFFFFFF= 32 times 1 , which is -1 in 2's compliement representation

    since ~ is a bitwise operation and turns zero to one in each bit:

    ~0b1010 == 0b0101
    

提交回复
热议问题