0

I like to ask question about in php as define below:

$a=0;
echo ~$a; //return -1;
$a=-2;
echo ~$a;//return 1;

what is reason behind this and how its work in php?

Arnold Daniels
  • 16,516
  • 4
  • 53
  • 82
Ashish4434
  • 118
  • 6
  • 4
    Read more http://php.net/manual/en/language.operators.bitwise.php – JustOnUnderMillions Apr 11 '17 at 11:03
  • 3
    It's a bitwise "not" operator, http://www.php.net/manual/en/language.operators.bitwise.php – Qirel Apr 11 '17 at 11:03
  • 5
    That's a bitwise `not`. It takes 0 which is 32 or 64 0s and makes them 32 or 64 1s which correspond to the number -1 (in [Two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation) – apokryfos Apr 11 '17 at 11:09
  • **This question is incorrectly marked as a duplicate.** It's clear that this is the bitwise not operator and how it works (probably even for the OP). However, the question is why is turn `0` into `-1` and not `PHP_INT_MAX` or any other value. This is briefly answered by @apokryfos, but deserves a full answer instead of only a link. – Arnold Daniels Apr 11 '17 at 12:06

0 Answers0