Php bit operations are not often used in php, but they are very useful. next we will introduce the usage of php bit operations. $ A & $ band (by bit and) $ a | $ bor (by bit or) $ a ^ $ bXor (by bit or )~ $ ANot (not by bit)
Php bit operations are not often used in php, but they are very useful. next we will introduce the usage of php bit operations.
$ A & $ B and (bitwise and)
$ A | $ B or (by bit or)
$ A ^ $ B Xor (bitwise Xor)
~ $ A Not (Not by bit)
$ A <$ B Shift left (left Shift)
$ A >>$ B Shift right (right Shift)
Details
$ A & $ B: Set the bitwise of 1 in both $ a and $ B to 1;
Example: 10 & 12 = 8
10 1010
12 1100
1000 8
$ A | $ B by bit or set one of $ a or $ B as 1;
Example: 10 | 12 = 14
10 1010
12 1100
1110 14
$ A ^ $ B by bit or
Example: 10 ^ 12
10 1010
12 1100
0110 6
~ A sets the value of 0 in $ a as 1, 1 as 0.
Example :~ 10 =
10 1010 1111111111111111111111111111111111111111111111111111111111110101-11
$ A <$ B move left to move $ B from $ a to the left (each move is multiplied by 2 );
Example: 1 <10 = 1024
1 (1) shift left 10 digits 10000000000 (1024)
It is equivalent to the 10 power of 1*2. it is really depressing to have no power operation in php.
$ A >> B B shifts the right of $ a to the right of $ B (each movement is divided by 2 );
Example: 1024 <2 = 1256
10000000000 (1024) shifted two places to the right is 100000000 (256)
Php calculates $ a & $ B and (bitwise and) $ a | $ B or (bitwise or) $ a ^ $ B Xor (bitwise or )~ $ A Not (Not by bit) $ a <$ B Shift left (left Shift) $ a >$ B Shift right (right Shift)
For details, set the bitwise of $ a & $ B to 1 in both $ a and $ B. For example, 10 & 12 = 810 101012 1100 8
$ A | $ B by bit or set one of $ a or $ B as 1; example: 10 | 12 = 1410 101012 1100 14
$ A ^ $ B: 10 ^ 1210 101012 1100 6
~ A sets the value of 0 in $ a as 1, 1, and 0 by bit. for example :~ 10 = 10 1010 1111111111111111111111111111111111111111111111111111111111110101-11
$ A <$ B shifts left to move $ B from $ a to the left. $ B (each movement represents 2). For example, 1 <10 = 10241 (1) moving 10 digits 10000000000 (1024) to the left is equivalent to 10 power of 1*2. it is really depressing that php does not have a power operation.
$ A >> B B shifts the right of $ a to the right of $ B (each movement is divided by 2). For example, 1024 <2 = 125610000000000 (1024) the 2-digit right shift is 100000000 (256)
Combination of flags and bitwise operators
List of error_reporting parameter values in PHP
Value constant
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
2047 E_ALL
2048 E_STRICT
4096 E_RECOVERABLE_ERROR
It is found that the values of values are all leaping, and they are all n + 1 power of 2.
Let's look at the following. convert the value to binary.
Value constant
0000 0001 E_ERROR
0000 0010 E_WARNING
0000 0100 E_PARSE
0000 1000 E_NOTICE
0001 0000 E_CORE_ERROR
0010 0000 E_CORE_WARNING.
... ... Adding a single square for each addition is a binary (almost everyone who has learned about the computer knows :)...)
Note: Each option corresponds to one (1 is enabled, 0 is disabled)
Now let's take a look at the benefits of setting parameters.
Let's take three parameters as an example to see what the results are.
Error_reporting (3); // decbin (3) = 0000 0011; (equivalent to setting E_WARNING and E_ERROR)
Error_reporting (4); // decbin (4) = 0000 0100; (equivalent to setting E_PARSE)
Error_reporting (5); // decbin (5) = 0000 0101; (equivalent to setting E_PARSE and E_ERROR)
Get settings:
It depends on whether or not a certain item is enabled and can be obtained by bitwise operations (&-"and" all rules 1 is 1, otherwise 0)
// E_PARSE
If ($ n & 4 ){
// Enable E_PARSE
// The binary value of 4 is 0100. because only 3rd bits are 1, all other locations are set to 0 during the "&" operation.
// Therefore, the result is greater than 0 only when the third digit of $ n is also 1.
// For example, 4 (0100), 5 (0101), 6 (0110), 7 (0111)
} Else {
// Close E_PARSE
// If the third digit is 0, this indicates that this option is disabled.
}
Change Settings: ($ n indicates the current decimal value)
During the application, we may set the switch for a specific user.
See the following usage.
// Disable the '&' and "rules for the E_PARSE item
$ N = $ n & (8192-4-1 );
// Why 8191?
// This is related to the number of options. this error indicates that a total of 13 bits are used (4096 of binary is 13 bits), and 8192 is (14 bits ).
// Why 4 minus 1?
// 8192-4-1 = 8187. (1111111111011) binary is 13 bits, which is the same as the maximum number of bits we use. The value on the third digit is 0.
// Use this number to calculate any number between 1 and 4096 by bitwise "and". Will the value of other bits remain unchanged except for the third bits? "And" rules :)
// Similarly, you want to disable E_WARNING.
// $ N = $ n & (8192-2-1 );
// Enable the '|' or "rule for the E_PARSE entry
// $ N = $ n | 4;
// Read the above close. I have some idea about enabling it :)
// '|'-'Or' has 1 as 1; otherwise, 0.
// If all bits are set to 1, other bits are not affected. If all bits are set to 0, other bits are not affected :)
// So we only need to set the corresponding value of the binary bit of the subsequent operand to 1, and the other positions will be OK.
// Found? Exactly the decimal value corresponding to each of our settings :)
This is the way it works. if you want to operate on the set values of multiple single-digit operations at the same time, it depends on how your operands are set.
In future development, we can consider this method when we need to set multiple options for one parameter at the same time :)