在MSB和LSB上写文章
Writing on MSB and on LSB of an unsigned Char
我有一个无符号字符,我想在四个最重要的四个最重要的情况下编写0x06
,我想在其4个最不重要的位置上编写0x04
。因此,字符表示应该像0110 0010
有些人可以指导我如何在C?
c = (0x06 << 4) | 0x04;
因为:
0x04 = 0000 0100
0x06 = 0000 0110
0x06<<4 = 0110 0000
or op: = 0110 0100
使用位移动运算符转移到正确的位置,并与位或。
结合unsigned char c = (0x6 << 4) | 0x4;
要扭转该过程并提取比特菲尔德,您可以使用bitwise 和使用掩码,其中只包含您感兴趣的位:
unsigned char lo4 = c & 0xf;
unsigned char hi4 = c >> 4;
首先,确保每 unsigned char
八位:
#include <limits.h>
#if CHAR_BIT != 8
#error "This code does not support character sizes other than 8 bits."
#endif
现在,假设您已经定义了一个unsigned char
:
unsigned char x;
然后,如果要完全设置unsigned char
在高四位中有6个,而在低四位中有4位,请使用:
x = 0x64;
如果您想查看a
的高位,而低位到b
,请使用:
// Shift a to high four bits and combine with b.
x = a << 4 | b;
如果要将高位设置为a
并保持低位不变,请使用:
// Shift a to high four bits, extract low four bits of x, and combine.
x = a << 4 | x & 0xf;
如果要将低位设置为b
并保持高位不变,请使用:
// Extract high four bits of x and combine with b.
x = x & 0xf0 | b;
上述a
和b
仅包含四位数值。如果他们可能设置了其他位,请分别使用(a & 0xf)
和(b & 0xf)
代替上面的a
和b
。
相关文章:
- 如何在整数中找到最低有效位(LSB)的位置?c ++
- 如何从__m64值的 lsb 创建 8 位掩码?
- 这篇文章对"passing shared_ptr in reference"是正确的吗?
- 在MSB和LSB上写文章
- 指针是否指向 LSB 或 MSB
- 调用模板化的成员函数:帮助我理解另一篇StackOverflow文章中的代码片段
- 引用类科学文章搜索错误
- 向数组中添加int,将LSB转换为0 c++
- JavaScript引擎开发书籍或文章
- 流异常文章
- 关于自由静态函数用法的文章
- 苹果的 Using C++ with Objective-C 文章已经消失了......谁能给我一篇新文章,或者帮我找到旧文章?
- 文章泛型<Programming>类型化缓冲区在 C++ 11 中是否完全过时?
- 我如何在GLUT中写一篇文章?
- 需要帮助理解这篇关于模板的文章
- 在MSB到LSB交换后保留符号和小数
- 如何将.pdf的文章摘要放入我的C++程序中
- C/ c++中最低有效位(LSB)和最高有效位(MSB)的校验值
- 本代码文章中的"缓存函数"指的是什么?
- 先将MSB转换为LSB