# Linux Kernel Module Cheat

### 29.6. x86 shift and rotate instructions

Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.5 "Shift and Rotate Instructions"

- SHL and SHR

  SHift left or Right and insert 0.

  CF == the bit that got shifted out.

  Application: quick unsigned multiply and divide by powers of 2.

- SAL and SAR

  Application: signed multiply and divide by powers of 2.

  Mnemonics: Shift Arithmetic Left and Right

  Keeps the same sign on right shift.

  Not directly exposed in C, for which signed shift is undetermined behavior, but does exist in Java via the `>>>` operator. C compilers can omit it however.

  SHL and SAL are exactly the same and have the same encoding: https://stackoverflow.com/questions/8373415/difference-between-shl-and-sal-in-80x86/56621271#56621271

- userland/arch/x86_64/rol.S: ROL and ROR

  Rotates the bit that is going out around to the other side.

- userland/arch/x86_64/rol.S: RCL and RCR

  Like ROL and ROR, but insert the carry bit instead, which effectively generates a rotation of 8 + 1 bits. TODO application.