Bit not operation in PHP(or any other language probably)

前端 未结 4 1225
予麋鹿
予麋鹿 2021-01-21 17:50

Why does this code return -1 ?

$a = 0; 
echo ~$a;

Isn\'t not suppose to revert the bits ?

4条回答
  •  耶瑟儿~
    2021-01-21 18:29

    Yes it should, and on the two's complement number system, a number with all bits set equals to -1 and since 0 has all bit unset, ~$a will have all its bit set.

    So the code is behaving as expected.

提交回复
热议问题