3.4.8 How Can I Rotate a Variable?

The C language does not have a rotate operator, but rotations can be performed using the shift and bitwise OR operators. Since the 32-bit devices have a rotate instruction, the compiler will look for code expressions that implement rotates (using shifts and ORs) and use the rotate instruction in the generated output wherever possible.

If you are using CCI, you should consult 25.3.10 Bitwise Operations on Signed Values and 25.3.11 Right-Shifting Signed Values if you will be using signed variables.

For the following example C code:

int rotate_left (unsigned a, unsigned s)
{
  return (a << s) | (a >> (32 - s));
}

the compiler may generate assembly instructions similar to the following:


rotate_left:
   subu      $2,$0,$5
   jr        $31
   ror       $2,$4,$2