0
点赞
收藏
分享

微信扫一扫

位运算的实践运用

炽凤亮尧 2022-03-12 阅读 65
c++

在这里插入图片描述

#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)
{
	this->Switch = 1 << (number-1);
	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
}
举报

相关推荐

0 条评论