C 从十进制中获取二进制信息

C 从十进制中获取二进制信息,c,C,假设指令aw是由32位结构定义的代码010,如下所示: bits 31-25 unused (all 0s) bits 24-22: code bits 21-19: argument 1 bits 18-16: argument 2 bits 15-0: offset (a 16-bit, 2's complement number with a range of -32768 to 32767) const uint32_t mask = 7 << 22; // Sh

假设指令
aw
是由32位结构定义的代码010,如下所示:

bits 31-25  unused (all 0s)
bits 24-22: code
bits 21-19: argument 1
bits 18-16: argument 2
bits 15-0:  offset (a 16-bit, 2's complement number with a range of -32768 to 32767)
const uint32_t mask = 7 << 22;    // Shift 3 set bits by 22 in order to build 
                                  // a mask where bits 22-24 are set.
const uint32_t inst_aw = 2 << 22; // Shift the opcode by 22 to build a comparable value

uint32_t instruction = ...;       // Your instruction word

if ((instruction & mask) == inst_aw) {
  // Do your thing
}
给定数字8454151,如何确定代码是否为
aw


我试图将数字移到22位,比如8454151>>22,但一直得到0。关于如何获取代码的位信息(检查它是
aw
还是其他内容)有什么想法吗。确保检查数据类型。您还可以将数字与0x01C000进行“和”,然后进行比较。

如果您只需验证某条指令是否属于特定操作,则需要最少周期的代码如下所示:

bits 31-25  unused (all 0s)
bits 24-22: code
bits 21-19: argument 1
bits 18-16: argument 2
bits 15-0:  offset (a 16-bit, 2's complement number with a range of -32768 to 32767)
const uint32_t mask = 7 << 22;    // Shift 3 set bits by 22 in order to build 
                                  // a mask where bits 22-24 are set.
const uint32_t inst_aw = 2 << 22; // Shift the opcode by 22 to build a comparable value

uint32_t instruction = ...;       // Your instruction word

if ((instruction & mask) == inst_aw) {
  // Do your thing
}

我更喜欢用一个常量来编码这个神奇的数字,或者定义从可理解的片段构建它(比如
7),确保在移位时使用4字节整数。