admin 管理员组

文章数量: 1184232

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

voidUtils::insertValueToQuint8CtrlWord(quint8* unsigned8CtrlWord,int high_position,int low_postion, quint16 setValue){int len = high_position - low_postion;switch(len){case0:
		setValue = setValue &0x00000001;break;case1:
		setValue = setValue &0x00000003;break;case2:
		setValue = setValue &0x00000007;break;case3:
		setValue = setValue &0x0000000f;break;case4:
		setValue = setValue &0x0000001f;break;case5:
		setValue = setValue &0x0000003f;break;case6:
		setValue = setValue &0x0000007f;break;case7:
		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类型数据中连续位读取数值

intUtils::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类型数据类似方法实现。

本文标签: 中连续位 类型数据 位操作技