What is the difference between E_ALL ^ E_NOTICE
and E_ALL & ~E_NOTICE
?
As I understand they both exclude error reporting of E_NOTICE
level. But in PHP.ini
the form &~
is used, while I've seen in many places the form ^
is mentioned.
There is a difference between ^
and &~
, but in this case there is no difference.
^
means XOR, so bits that are set in either the left operand or the right operand but not both are set in the result. &~
means AND NOT, so bits that are set in the left operand but not in the right operand are set in the result.
If we looked at the following numbers, there would be a difference:
$a = 0b1010; // decimal 10
$b = 0b0001; // decimal 1
$a ^ $b
results in 0b1011
(decimal 11) whereas $a & ~$b
results in 0b1010
(decimal 10).
However, E_ALL
is effectively 0b11111111
(i.e. all the bits are set; it's actually rather more than 8 bits). So XOR is effectively the same as AND NOT.
$a = 0b11111111; // decimal 255
$b = 0b00000100; // decimal 4
$a ^ $b
gives 0b11111011
(decimal 251), as does $a & ~$b
.
The first notation is a XOR between E_ALL
and E_NOTICE
. The second notation is a bitwise AND
with a negation operator applied to E_NOTICE
. The two conditions are technically not equivalent, since the first XOR
can be rewritten as ((E_ALL & ~E_NOTICE) | (~E_ALL & E_NOTICE))
.
Reference: php.net/manual/en/language.operators.bitwise.php
EDIT for completeness: as Jack and lonesomeday pointed out, since E_ALL has all the relevant bits set to 1, you are in the special case in which the two conditions are equivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With