phpBit operation is not commonly used in php, but its effect is quite large. Let’s introduce the usage of php bit operation.
$a & $b and(bitwise AND)
$a | $b or(bitwise OR)
$a ^ $b Xor(bitwise XOR)
~$a Not(bitwise not)
$a << $b Shift left(shift left)
$a >> $b Shift right(shift right Shift)
Detailed explanation
$a & $b Bitwise AND sets the bits in $a and $b that are both 1 to 1;
例:10 & 12 = 8
10 1010
12 1100
1000 8
$a | $b Bitwise OR sets one of $a or $b to 1;
Example: 10 | 12 = 14
10 1010
12 1100
1110 14
$a ^ $b Bitwise XOR
Example: 10 ^ 12
10 1010
12 1100
0110 6
~a Bitwise notation sets the 0 in $a to 1 and the 1 to 0
Example:~10 =
10 1010 11111111111111111111111111111111111111111111111111111 110101 - 11
$a << $b left shift moves the value in $a to the left $b times (each move means multiplying by 2);
Example: 1 << 10 = 1024
1(1) Left shift 10 digits 10000000000(1024)
Equivalent to 1*2 raised to the 10th power. It is really depressing that there is no power operation in PHP.
$a >> $b Right shift moves the value in $a to the right $b times (each move means dividing by 2);
Example: 1024 << 2 = 1256
10000000000(1024) Shifting right by 2 bits is 100000000(256)
php is the operation $a & $b and (bitwise AND) $a | $b or (bitwise OR) $a ^ $b Xor( Bitwise XOR)~$a Not (bitwise not)$a << $b Shift left(shift left)$a >> $b Shift right(shift right)
Detailed explanation$a & $ b Bitwise AND sets the bits that are both 1 in $a and $b to 1; Example: 10 & 12 = 810 101012 1100 1000 8
$a | $b Bitwise OR sets the bits in $a or $b A value of 1 is set to 1; example: 10 | 12 = 1410 101012 1100 1110 14
$a ^ $b Bitwise XOR example: 10 ^ 1210 101012 1100 0110 6
~a Bitwise NOT The 0 in $a is set to 1, and the 1 is set to 0. Example: ~10 = 10 1010 11111111111111111111111111111111111111111111111111111110101 -11
$a << $b Move left to the Move left $b times (each move means multiplying by 2); example: 1 << 10 = 10241(1) Shifting left by 10 digits 10000000000(1024) is equivalent to 1*2 raised to the 10th power. There is no exponentiation operation in PHP It's really depressing.
$a > Shifting 2 bits is 100000000(256)
The combination of the flag field and the bit operator
in PHP Parameter value list for error_reporting
0000 0001 E_ERROR
0000 0010 E_WARNING0000 0100 E_PARSE
0000 1000 E_NOTICE
0001 0000 E_CORE_ERROR
0010 0000 E_CORE_WARNING
.
.
.
... ...Every time one is added to the power, one digit is added to the binary system (almost everyone who has studied computers knows:)...)
Note: Each option corresponds to one digit (1 is Turn on 0 to turn off)
Okay, let’s take a look at the benefits of setting parameters like this.
error_reporting(3);//decbin(3) == 0000 0011; (相当于设置 E_WARNING 和 E_ERROR ) error_reporting(4);//decbin(4) == 0000 0100;(相当于设置 E_PARSE ) error_reporting(5);//decbin(5) == 0000 0101;(相当于设置 E_PARSE 和 E_ERROR)
if($n & 4){
//E_PARSE is turned on//The binary of 4 is 0100, because only the third bit is 1, so when performing the "&" operation
All other
positions are set to 0
//So the result will only be when the third bit of $n is also 1 Greater than 0. //Such as 4(0100),5(0101),6(0110),7(0111)}else{
//E_PARSE is closed
//The third bit is 0 Indicates that this option is closed
}
Change settings: ($n represents the current decimal value)
See usage below.
//Close the E_PARSE item and use the ‘&’ “AND” rule
$n = $n&(8192-4-1);
//Why use 8191?
//This has something to do with the number of options you have. This error display mark uses a total of 13 bits (the binary number of 4096 is 13 bits), while 8192 is (14 bits).
//Why subtract 4 and subtract 1 Woolen cloth?
//8192-4-1=8187. (1111111111011) The binary number is 13 digits, which is the same as the maximum number of digits we use. And the corresponding value in the third bit is 0.
//Use this number to perform a bitwise AND operation with any number between 1 and 4096. Except for the third bit, which will be set to 0, the values of other bits will not change? "AND" rule:)
//Similarly, if you want to turn off E_WARNING
//$n = $n&(8192-2-1);
//To turn on the E_PARSE item, use ' |'"or" rule
$n = $n|4;
//After reading the above closing, you may have some ideas about opening:)
// '|' — "or" rule If there is 1, it is 1, otherwise it is 0
//The above is the case where all bits are 1, it does not affect other bits, now it becomes the case where all bits are 0, it will not affect other bits:)
// So we only need to set the corresponding value of the binary bit of the subsequent operand to 1, and all other bits to 0 will be OK.
//Did you find it? It happens to be the decimal value corresponding to each of our setting items:
The above is the detailed content of Introduction to the usage of php median operation. For more information, please follow other related articles on the PHP Chinese website!