PHP operators have a class of bit operations, this article and we share the PHP bit operation in detail, I hope to help everyone.
One:& and bitwise with
$a & $b will set the binary $ A and binary $b to 1 and the other bits to 0.1
For example:
7&3=>7 (0111) &3 (0011) can be seen, 0111 and 0011 are 1 of only the back 2 1, then 7 (0111) &3 (0011) = 0011; 110 binary is 3, then 7&3=3
One practical use of bitwise and is to judge parity $a &1
Principle Analysis:
1 binary only the last one is 1, the other bits are 0, and the last of the odd is 1,
For example 11&1=>11 (1011) &1 (0001) = 1 is odd, 12&1=>12 (1100) &1=0 is even
This method determines the high performance of odd even ratio $a%2==1.
Two: | or bitwise-SAME OR
$a | $b will set $ A and any of the $b as 1 bits to 1.
Example Ibid.
Three: ^ XOR bitwise XOR
$a ^ $b will set a bit of $ A and $b to 1 for one of 1 and the other for 0.
Example Ibid.
FOUR: ~ Not bitwise reversed
$a ~ $b sets the bit in $ A to 1 and vice versa.
Example Ibid.
V: << Shift Left
$a << $b; moves the bits in $ A to the left $b times (each move represents "multiplied by 2").
Bit-based operations are faster than multiplication operations, so $a*2 can be written in $a<<1
Six: >>shift right (move to the left)
$a >> $b; moves the bits in $ A to the right $b times (each move represents "multiplied by 2").
Bit-based operations are faster than multiplication operations, so $A/2 can be written in $a>>1
Related recommendations:
Example of a PHP bitwise operator permission operation
Examples summarize the use of PHP bit operators
PHP bit operator Usage example detailed