如何添加一点半字节 [英] How to add a bit with nibble
本文介绍了如何添加一点半字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi
我想在半字节中添加一个比特,因为我想通过在每个数据块中添加一个额外的比特将8比特数据分段为2个4比特半字节,并且在接收端执行将接收数据5位的形式,之后将删除额外的位和Assemble 2半字节的
Hi I want to add a single bit in the nibble as i want to fragment 8 bits data into 2 4 bit nibble's by adding an extra bit to each of them and do on the reception side will receive data in the form of 5 bit, after that will remove the extra bit and Assemble 2 nibble's
推荐答案
首先,创建一个第N位设置的数字,所有其他位清除。该值1<< N
。
下一步,取出原始值并使用上一步的值计算按位OR。
即:
First, create a number with N-th bit set and all other bits clear. This value is1 << N
.
On next step, take your original value and calculate the bitwise OR with the value from the previous step.
That is:
int N = //... bit number
int value = //... can be any integer type, signed or unsigned
value |= 1 << N; // same as:
value = value | (1 << N)
清除一下,使用按位AND和补码:
To clear a bit, use bitwise AND with the complement:
value &= ~(1 << N);
这就是全部。学习按位运算符: http://www.learncpp.com/cpp-tutorial/38-bitwise-operators [ ^ ]。< br $>
这篇关于如何添加一点半字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文