【发布时间】:2020-09-16 14:59:49
【问题描述】:
我有一个需要操作的 32 位寄存器。但我只需要更改 12-15 位,其余位保持不变。
我想用 0x2 或 0b0010 覆盖位 12-15 中的任何内容。
如何在 C++ 中做到这一点?
这是我一直在尝试的示例代码。
#include <iostream>
#include <bitset>
using namespace std;
int main() {
uint32_t x = 0x4D00D0F0;
x |= 0x2 << 12; // shifting 0x2 12 bits to the left
return 0;
}
我的解决方案似乎只是将 1 移到第 13 位,这是为什么呢?
我可以移位整个 4 位十六进制数吗?
底线,我想要 x = 0x4D0020F0
【问题讨论】: