PHP bit operations are not commonly used in PHP, but the role is quite large, let us introduce the PHP bit operation usage.
$a & $b and (bitwise AND)
$a | $b or (bitwise OR)
$a ^ $b Xor (Bitwise XOR)
~ $a not (bitwise non)
$a << $b shift left
$a >> $b shift right
Detailed
$a & $b Bitwise and set the bit to 1 in both $ A and $b to 1;
Example: & 12 = 8
10 1010
12 1100
1000 8
$a | $b bitwise OR put a $ A or $b in one of 1 for the set to 1;
Example: 10 | 12 = 14
10 1010
12 1100
1110 14
$a ^ $b Bitwise XOR OR
Example: 10 ^ 12
10 1010
12 1100
0110 6
~a is set to 0 for the 0 of the per-digit
Example: ~ =
10 1010 1111111111111111111111111111111111111111111111111111111111110101-11
$a << $b left move the $b of $ A to the left (each move represents a multiply by 2);
Example: 1 << 10 = 1024
1 (1) shift left 10 bit 10000000000 (1024)
Equivalent to the 1*2 10 times, PHP is not a power operation is really depressed.
$a >> $b Right Move the $b in $ A to the right (each move is represented by 2);
Example: 1024x768 << 2 = 1256
10000000000 (1024) Shift right 2 bits is 100000000 (256)
PHP for Operation $ A & $b and (bitwise VS) $a | $b or (bitwise OR) $a ^ $b Xor (Bitwise XOR) ~ $a not (bitwise non) $a << $b shift left $a >> $b shift right
Detailed $ A & $b bitwise set to 1 for Bits 1 in $ A and $b; Example: & 12 = 810 101012 1100 1000 8
$a | $b bitwise OR put a $ A or $b in one of 1 for the set to 1; Example: 10 | 12 = 1410 101012 1100 1110 14
$a ^ $b Bitwise XOR: 10 ^ 1210 101012 1100 0110 6
~a is set to 0 for the 0 of the per-bit non-set of $ A: ~ = 10 1010 1111111111111111111111111111111111111111111111111111111111110101-11
$a << $b left move the $b in $ A to the left (each move is multiplied by 2), for example: 1 << 10 = 10241 (1) Shift left 10 bits 10000000000 (1024) is equivalent to 1*2 10 times, PHP is not a power operation is really depressed.
$a >> $b Right Move the $b in $ A to the right (each move is represented by 2); For example: 2 << 2 = 125610000000000 (1024) Right-shift to the left-hand side is 100000000 (256)
The combination of the flag bit field and the bitwise operator
List of parameter values for error_reporting in PHP
Value constant
1 E_error
2 e_warning
4 E_parse
8 E_notice
E_core_error
E_core_warning
E_compile_error
E_compile_warning
E_user_error
E_user_warning
1024x768 E_user_notice
2047 E_all
2048 e_strict
4096 E_recoverable_error
The values of value are found to be jumping, and are all 2 n+1.
Look at the following. The value is converted to binary.
Value constant
0000 0001 E_error
0000 0010 E_warning
0000 0100 E_parse
0000 E_notice
0001 0000 E_core_error
0010 0000 e_core_warning
.
.
.
... ... Once for each plus side is the binary added one (learned computer of almost all know:) ... )
Note: Each option corresponds to one bit (1 turns on 0 for off)
Well, let's look at the benefits of setting this parameter.
Take three parameters as an example to see what the effect is.
Error_reporting (3);//decbin (3) = = 0000 0011; (equivalent to Setting e_warning and E_error)
Error_reporting (4);//decbin (4) = = 0000 0100; (equivalent to set E_parse)
Error_reporting (5);//decbin (5) = = 0000 0101; (equivalent to Setting e_parse and E_error)
Get settings:
To see whether an item is turned on can be obtained by bitwise operation (&-"and" rule all 1 is 1, otherwise 0)
E_parse
if ($n & 4) {
E_parse Open
4 of the binary is 0100, because only the 3rd bit is 1, so the "&" operation when the other position is all set 0.
So only the third bit of $n is also 1 o'clock results will be greater than 0.
e.g. 4 (0100), 5 (0101), 6 (0110), 7 (0111)
}else{
E_parse off
The third bit is 0, which means this option is off state
}
Change settings: ($n represents the current decimal value)
At the time of application we may need to switch settings for a certain bit.
Look at the following usage.
Close E_parse items with ' & ' and ' rules
$n = $n & (8192-4-1);
Why use 8191?
This is related to the number of options you have, the error indicator is 13 bits (4096 binary is 13 bits), and 8192 is (14 bits).
Why minus 4 minus 1?
8192-4-1=8187. (1111111111011) The binary is 13 bits, the same as the maximum number of digits we use. And the value on the corresponding third bit is 0.
With this number and any number between 1 and 4096, the bitwise "and" operation is not the same as the third digit will set 0, the value of the other bits will not change? "and" rule:)
In the same vein, you want to turn off e_warning
$n = $n & (8192-2-1);
Open E_parse for ' | ' or the Rules
$n = $n |4;
Look at the closure above, for the opening a little bit of thought about it:)
' | '-"or" rule has 1 for 1 otherwise 0
The above is the case where all bits are 1 without affecting the other bits, and now it becomes all bits 0 without affecting the other bits:)
So we just put the bits corresponding value of the following operand 1, all other positions 0 is OK.
Did you find it? is exactly the decimal value for each of our setup items:)
The idea is this, if you want to manipulate the settings on multiple bits at the same time, it depends on how you set the operand.
We can consider this method in the future when we encounter a situation where a parameter is required to set multiple options at the same time:)
http://www.bkjia.com/PHPjc/629039.html www.bkjia.com true http://www.bkjia.com/PHPjc/629039.html techarticle PHP bit operations are not commonly used in PHP, but the role is quite large, let us introduce the PHP bit operation usage. $a $b and (bitwise AND) $a | $b or (bitwise OR) $a ^ $b Xor (Bitwise XOR) ...