# Bitwise operator -- switch state of management transaction

1. What is switch state

In reality, there are a lot of data with only two results (values), corresponding to our Boolean values.

Here, the so-called switch state of managing a group of transactions should be understood as managing several "data symbols" with only two states.

For example, there are 5 bulbs, corresponding to 5 status data. These five bulbs have 25 states

The management goal here is to express the "current state" of several data with one variable. There are three tasks:

1. Through this variable, you can get the current status of any data (bulb).

2. Through this variable, you can "close" the status of one data;

3. Through this variable, you can "turn on" the status of one data;

```<?php
//Suppose that there are 5 bulbs to be managed and the following functional objectives need to be achieved
//1. You can specify the "current state" of any bulb
//2. Any specified bulb can be turned on

//First, you need to set the corresponding 5 constants to represent the corresponding 5 bulbs

define("D1" ,1);    //The corresponding binary value is: 00000001
define("D2" ,2);    //The corresponding binary value is: 00000010
define("D3" ,4);    //The corresponding binary value is: 00000100
define("D4" ,8);    //The corresponding binary value is: 00001000
define("D5" ,16);   //The corresponding binary value is: 00010000
//More lights, also define constant value according to this rule!!!

//Then, a variable is defined, which represents the "any combination state" value of the five bulbs;
\$state = 10;   //Its corresponding binary value is: 00001010
//At this time, it also means that the second and fourth lights are cool
//\$state =7; / / its corresponding binary value is: 00000 111
//\$state =71; / / its corresponding binary value is: 00010001
//Note: the above three assignments only indicate that the value of \$state can be "arbitrary"

//Task 1, you can specify the "current state" of any bulb

//Requirement 1a: Please enter the status of bulb 1
//According to this algorithm logic, light 1 can be judged
//Analysis:
//The value of \$state is: 00001010
//The value of D1 is: 00000001
//              &
//------------------------
//              00000000
if((\$state & D1) >0){

echo "<br />Light 1 is on";
}else
{
echo "<br />Light 1 is off";
}

//Demand 1 b: Please enter the status of bulb 2;
if((\$state & D2) >0){

echo "<br />Light 2 is on";
}else
{
echo "<br />Light 2 is off";
}

//Write a method to show the overall display of all lights
function ShowAll()
{
echo "<p>";
for(\$i=1;\$i<=5;++\$i)
{
\$s = "D".\$i;
if((\$GLOBALS['state'] & constant(\$s)) > 0)
{
echo "lamp{\$i}It's bright.<br />";
}else
{
echo "lamp{\$i}It is extinguished.<br />";
}
}
echo "</p>";
}
echo "<br />Initial multi lamp state:";
ShowAll();

//Requirement 2, any specified bulb can be turned on
//It can also be opened as long as the following algorithm is followed:
//\$state = \$state| constant value of corresponding lamp:

//Requirement 2a: please turn on light 3
\$state = \$state | D3;
echo "<br />When light 3 is on:";
ShowAll();

//Demand 2 b: Please turn on light 5:
\$state = \$state | D5;
echo "<br />When light 5 is on:";
ShowAll();

//Requirement 3, any specified bulb can be turned off
//It can also be opened as long as the following algorithm is followed:
//\$state = \$state & (~ constant value of corresponding lamp):

//Demand 3a: please turn off light 2
\$state = \$state & (~D3);
echo "<br />When light 3 is off:";
ShowAll();

//Demand 3 b: Please turn off light 5
\$state = \$state & (~D5);
echo "<br />After light 5 is off:";
ShowAll();

//Demand 3 c: Please turn off light 1 (in fact, the board is turned off)
\$state = \$state & (~D1);
echo "<br />When light 1 is off:";
ShowAll();```

Tags: PHP

Posted on Wed, 01 Apr 2020 10:17:52 -0700 by daijames