Here is an expression that produces a syntax error:

opcode >>> temp; 

In C ++, there is no built-in unsigned right shift operator? Need to do it yourself with masks?

  • four
    ((unsigned)opcode) >> temp ? - VladD
  • What is an unsigned shift, and why is it needed? - Abyx
  • @Abyx: It does not propagate the sign bit, EMNIP. - VladD
  • @Abyx when the high-order bit is filled with zeros, regardless of whether the number was positive or negative. Well, I get the machine instruction as a whole number of 32 bits. I need to get an opcode - this is the older 5 bits. But then I need to show this opcode as if it is an unsigned number of 5 bits. Therefore, they need to be shifted 27 signs to the right. The resulting number will be opcode. But this is if the high-order bits are filled with zeros and will not affect the number. - Alexander Elizarov
  • one
    @Alexander Elizarov: It must be something like this: struct Instruction { unsigned int opcode : 5; unsigned int flags: 1; ... }; ... Instruction inst; ... opcode = inst.opcode; struct Instruction { unsigned int opcode : 5; unsigned int flags: 1; ... }; ... Instruction inst; ... opcode = inst.opcode; - VladD

1 answer 1

Whether a signed or unsigned shift is applied depends on the sign of the left operand. So just bring it to the required type.

And note that in the right shift of the sign type, the distribution of the sign bit (to distribute it or fill it with zeros) depends on the compiler.

True, I have not yet heard that the sign bit in this situation did not spread somewhere ...

 int main(int argc, const char * argv[]) { int opcode = 0xFF000000; cout << (opcode >> 27) << endl; cout << ((unsigned int)opcode >> 27) << endl; } 

gives (VC ++ 2015)

 -1 31