Circular shifts are used often in cryptography in order to permute bit sequences. Unfortunately, many programming languages, including C, do not have operators or standard functions for circular shifting, even though virtually all processors have bitwise operation instructions for it (e.g. Intel x86 has ROL and ROR). However, some compilers may provide access to the processor instructions by means of intrinsic functions. In addition, some constructs in standard ANSI C code may be optimized by a compiler to the "rotate" assembly language instruction on CPUs that have such an instruction. Most C compilers recognize the following idiom, and compile it to a single 32-bit rotate instruction.12
This safe and compiler-friendly implementation was developed by John Regehr,3 and further polished by Peter Cordes.45
A simpler version is often seen when the count is limited to the range of 1 to 31 bits:
This version is dangerous because if the count is 0 or 32, it asks for a 32-bit shift, which is undefined behaviour in the C language standard. However, it tends to work anyway, because most microprocessors implement value >> 32 as either a 32-bit shift (producing 0) or a 0-bit shift (producing the original value), and either one produces the correct result in this application.
If the bit sequence 0001 0111 were subjected to a circular shift of one bit position... (see images below)
If the bit sequence 1001 0110 were subjected to the following operations:
Cyclic codes are a kind of block code with the property that the circular shift of a codeword will always yield another codeword. This motivates the following general definition: For a string s over an alphabet Σ, let shift(s) denote the set of circular shifts of s, and for a set L of strings, let shift(L) denote the set of all circular shifts of strings in L. If L is a cyclic code, then shift(L) ⊆ L; this is a necessary condition for L being a cyclic language. The operation shift(L) has been studied in formal language theory. For instance, if L is a context-free language, then shift(L) is again context-free.67 Also, if L is described by a regular expression of length n, there is a regular expression of length O(n3) describing shift(L).8
GCC: "Optimize common rotate constructs" https://gcc.gnu.org/ml/gcc-patches/2007-11/msg01112.html ↩
"Cleanups in ROTL/ROTR DAG combiner code" mentions that this code supports the "rotate" instruction in the CellSPU http://www.mail-archive.com/llvm-commits@cs.uiuc.edu/msg17216.html ↩
Safe, Efficient, and Portable Rotate in C/C++ http://blog.regehr.org/archives/1063 ↩
Stackoverflow: Best practices for rotates in C/C++ https://stackoverflow.com/a/776523/224132 ↩
Near constant time rotate that does not violate the standards https://stackoverflow.com/a/31488147/224132 ↩
T. Oshiba, "Closure property of the family of context-free languages under the cyclic shift operation", Transactions of IECE, 55D:119–122, 1972. ↩
A. N. Maslov, "Cyclic shift operation for languages", Problems of Information Transmission 9:333–338, 1973. ↩
Gruber, Hermann; Holzer, Markus (2009). "Language operations with regular expressions of polynomial size". Theoretical Computer Science. 410 (35): 3281–3289. doi:10.1016/j.tcs.2009.04.009. Zbl 1176.68105.. https://doi.org/10.1016%2Fj.tcs.2009.04.009 ↩