#include<iostream>
#include<bitset>
class Light{
private:
unsigned int Switch;
public:
Light(unsigned int S)
{
this->Switch = S;
}
void ControlEveryL(unsigned int number);
void OpenOneBlock(unsigned int target);
void CloseOneBlock(unsigned int target);
~Light() {};
};
void Light::ControlEveryL(unsigned int number)
{
unsigned k = 0;
k= 1 << (number-1);
this->Switch = this->Switch | k;
std::cout << std::bitset<32>(this->Switch)<<(char)10;
}
void Light::OpenOneBlock(unsigned int target)
{
//开指定区域
unsigned int k = 0xff;
k = k << 8*(target-1);
this->Switch = this->Switch | k;
std::cout << std::bitset<32>(this->Switch) << (char)10;
}
void Light::CloseOneBlock(unsigned int target)
{
//关闭指定区域
unsigned int k = 0xff;
k = k << 8 * (target - 1);
this->Switch = this->Switch & (~k);
std::cout << std::bitset<32>(this->Switch) << (char)10;
}
int main()
{
Light L1{2};
//获取所有灯光状态 &0xffffffff
//一次性打开|0xffffffff 关闭 &0
}
位运算的实践运用
于 2022-03-10 20:19:56 首次发布