C++中按位写入读取数值-创新互联

向 quint8类型数据中连续位写入数值

目前成都创新互联已为上千的企业提供了网站建设、域名、网站空间、网站托管、服务器托管、企业网站设计、印江网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
void Utils::insertValueToQuint8CtrlWord(quint8* unsigned8CtrlWord, int high_position, int low_postion, quint16 setValue)
{int len = high_position - low_postion;
	switch (len)
	{case 0:
		setValue = setValue & 0x00000001;
		break;
	case 1:
		setValue = setValue & 0x00000003;
		break;
	case 2:
		setValue = setValue & 0x00000007;
		break;
	case 3:
		setValue = setValue & 0x0000000f;
		break;
	case 4:
		setValue = setValue & 0x0000001f;
		break;
	case 5:
		setValue = setValue & 0x0000003f;
		break;
	case 6:
		setValue = setValue & 0x0000007f;
		break;
	case 7:
		setValue = setValue & 0x000000ff;
		break;
	default:
		break;
	}

	bitset<32>binaryControlWord(*unsigned8CtrlWord);
	for (int i = low_postion; i<= high_position; i++)
	{binaryControlWord.reset(i);
	}

	bitset<32>binaryValue(setValue);
	binaryValue<<= low_postion;
	binaryControlWord |= binaryValue;
	*unsigned8CtrlWord = (quint32)binaryControlWord.to_ulong();
}

从 quint8类型数据中连续位读取数值

int Utils::getValueFromQuint8CtrlWord(quint8* unsigned8CtrlWord, int high_position, int low_postion)
{int ret = 0;

	bitset<16>binaryCtrlWord(*unsigned8CtrlWord);

	for (int i = 0; i< low_postion; i++) binaryCtrlWord.reset(i);
	for (int i = high_position + 1; i<= 7; i++) binaryCtrlWord.reset(i);

	binaryCtrlWord >>= low_postion;

	ret = (quint8)binaryCtrlWord.to_ulong();

	return ret;
}

说明:quint16,quint32,quint64类型数据类似方法实现。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享标题:C++中按位写入读取数值-创新互联
文章地址:http://cdiso.cn/article/ddohgg.html

其他资讯