我想知道是否有在 if 语句中使用特定范围的位。 我正在使用 fpga 通过 usb 将 8 位二进制数据发送到 pc。每个事务有 3x 8 位数据包。在每个数据包中,前四位由外部模块生成,我想在最后四位中发送控制数据。 => USB 接口(interface)接受数据作为整数,我有 bitset 函数将整数转换为 8 位二进制。我想使用最后四位与 if 语句一起使用。我有什么办法可以做到这一点吗?
提前致谢
最佳答案
如果您有一个 unsigned char
作为输入:
void foo(uint8_t x) {
uint8_t top4 = x >> 4; // moves top 4 bits down by 4 positions
uint8_t bottom4 = x & 0x0f; // zeros out top 4 bits, leaving bottom 4
}
这也适用于 int
、short
等类型,但您需要 &
-mask top
结果也去除了不需要的高位。
关于C++ 位集与 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23788470/