Bitwise operators-manage the Switch Status of transactions and operator transaction Switches
1. What is the switch status?
In reality, there are many data with only two results (values), which correspond to our boolean values.
Here, the so-called management of the switch status of a group of transactions should be understood as actually managing several "data symbols" with only two States ".
For example, five bulbs correspond to five State data. The five bulbs are in 25 states.
The management goal here is to use a variable to express the "current state" of several data items ". There are three specific tasks:
1. With this variable, you can know the current status of any data (light bulb.
2. With this variable, you can "close" The status of one data item ";
3. With this variable, you can "enable" the status of each data item ";
<? Php // assume that five bulbs need to be managed and the following functional goals must be achieved // 1. You can specify the "current status" of any bulbs. // 2, you can open any specified bulb. // first, you need to set the corresponding five constants to represent the corresponding five bulb. define ("D1", 1 ); // The binary value is 00000001 define ("D2", 2); // The binary value is 00000010 define ("D3", 4 ); // The binary value is 00000100 define ("D4", 8); // The binary value is 00001000 define ("D5", 16 ); // The corresponding binary value is 00010000 // more lights. The constant value is also defined according to this rule !!! // Then, define a variable that represents the "any combination state" value of the five bulbs; $ state = 10; // the corresponding binary value is: 00001010 // at this time, it also indicates that 2nd and 4th lights are cooled // $ state = 7; // the corresponding binary value is: 00000111 // $ state = 71; // The binary value is 00010001 // Note: The above three values are assigned, it only indicates that the value of $ state can be "arbitrary" // Task 1, and the "current state" of any light bulb can be specified // requirement 1a: enter the status of light bulb 1 // according to the logic of this algorithm to determine the value of light 1 // analysis: // $ state: 00001010 // D1: 00000001 // ------------------------ // specify when if ($ state & D1)> 0) {echo "<br/> the light 1 is on ";} else {echo "<br/> Lamp 1 is off";} // requirement 1b: Enter the status of the light bulb 2. if ($ state & D2)> 0) {echo "<br/> the light 2 is on ";} else {echo "<br/> Lamp 2 is off";} // write method, showing the overall display function ShowAll () {echo "<p> "; for ($ I = 1; $ I <= 5; ++ $ I) {$ s = "D ". $ I; if ($ GLOBALS ['state'] & constant ($ s)> 0) {echo "Light {$ I} is on <br/> ";} else {echo "lamp {$ I} is off <br/>" ;}} echo "</p> ";} echo "<br/> initial status with multiple lights:"; ShowAll (); // requirement 2, you can enable any specified light bulb. // you can enable it by following the following algorithm: // $ state = $ state | constant value of the corresponding light: // requirement 2a: turn on the light 3 $ state = $ st Ate | D3; echo "<br/> after Lamp 3 is turned on:"; ShowAll (); // requirement 2b: Turn On Lamp 5: $ state = $ state | D5; echo "<br/> after Lamp 5 is turned on:"; ShowAll (); // requirement 3: You can turn off any specified bulb. // you can enable it by following the following algorithm: // $ state = $ state &(~ The constant value of the corresponding lamp): // requirement 3a: Turn off the lamp 2 $ state = $ state &(~ D3); echo "<br/> after Lamp 3 is turned off:"; ShowAll (); // requirement 3b: Turn off Lamp 5 $ state = $ state &(~ D5); echo "<br/> after Lamp 5 is turned off:"; ShowAll (); // required 3c: Turn off Lamp 1 (in fact, it was originally turned off) $ state = $ state &(~ D1); echo "<br/> after Lamp 1 is off:"; ShowAll ();