Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between E_ALL ^ E_NOTICE and E_ALL & ~E_NOTICE

Tags:

php

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.

like image 408
Max Koretskyi Avatar asked Mar 21 '23 08:03

Max Koretskyi


2 Answers

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.

like image 108
lonesomeday Avatar answered Apr 04 '23 02:04

lonesomeday


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.

like image 38
Andrea Sprega Avatar answered Apr 04 '23 03:04

Andrea Sprega