Circular shift

From formulasearchengine
Jump to navigation Jump to search

{{ safesubst:#invoke:Unsubst||$N=Refimprove |date=__DATE__ |$B= {{#invoke:Message box|ambox}} }}

Loupe light.svg Array of circular shifts to the left   (OEISA051683)
Corresponding inversion vectors have only one non-zero entry.

In combinatorial mathematics, a circular shift is the operation of rearranging the entries in a tuple, either by moving the final entry to the first position, while shifting all other entries to the next position, or by performing the inverse operation. A circular shift is a special kind of cyclic permutation, which in turn is a special kind of permutation. Formally, a circular shift is a permutation σ of the n entries in the tuple such that either

modulo n, for all entries i = 1, ..., n,


modulo n, for all entries i = 1, ..., n.

The result of repeatedly applying circular shifts to a given tuple are also called the circular shifts of the tuple.

For example, repeatedly applying circular shifts to the four-tuple (a, b, c, d) successively gives

  • (d, a, b, c),
  • (c, d, a, b),
  • (b, c, d, a),
  • (a, b, c, d) (the original four-tuple),

and then the sequence repeats; this four-tuple therefore has four distinct circular shifts. However, not all n-tuples have n distinct circular shifts. For instance, the 4-tuple (a, b, a, b) only has 2 distinct circular shifts. In general the number of circular shifts of an n-tuple could be any divisor of n, depending on the entries of the tuple.

In computer programming, a circular shift (or bitwise rotation) is a shift operator that shifts all bits of its operand. Unlike an arithmetic shift, a circular shift does not preserve a number's sign bit or distinguish a number's exponent from its mantissa. Unlike a logical shift, the vacant bit positions are not filled in with zeros but are filled in with the bits that are shifted out of the sequence.

Implementing circular shifts

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, it is possible to write standard ANSI C code that compiles down to the "rotate" assembly language instruction (on CPUs that have such an instruction). Most C compilers recognize this idiom:

  unsigned int x;
  unsigned int y;
  /* ... */
  y = (x << shift) | (x >> (sizeof(x) * CHAR_BIT - shift));

and compile it to a single 32-bit rotate instruction.[1][2]

On some systems, this may be "#define"ed as a macro or defined as an inline function called something like "rightrotate32", "rotr32" or "ror32" in a standard header file like "bitops.h".[3] Rotates in the other direction may be "#define"ed as a macro or defined as an inline function called something like "leftrotate32", "rotl32" or "rol32" in the same "bitops.h" header file.

If necessary, circular shift functions can be defined (here in C):

/* We don't need special handling for certain shift values:
 * Shift operations in C are only defined for shift values which are
 * not negative and smaller than sizeof(value), but the compiler
 * will generate correct code for the following code pattern. */

unsigned int rotl(unsigned int value, int shift) {
    return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift));

unsigned int rotr(unsigned int value, int shift) {
    return (value >> shift) | (value << (sizeof(value) * CHAR_BIT - shift));


If the bit sequence 0001 0111 were subjected to a circular shift of one bit position... (see images below)

  • to the left would yield: 0010 1110
Left circular shift.

  • to the right would yield: 1000 1011.
Right circular shift.

If the bit sequence 0001 0111 were subjected to a circular shift of three bit positions...

  • to the left would yield: 1011 1000
  • to the right would yield: 1110 0010.


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. As already noted, if L is a cyclic code, we have L = shift(L). 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.[4][5] Also, if L is described by a regular expression of length n, there is a regular expression of length O(n³) describing shift(L).[6]

See also


  1. GCC: "Optimize common rotate constructs"
  2. "Cleanups in ROTL/ROTR DAG combiner code" mentions that this code supports the "rotate" instruction in the CellSPU
  3. "replace private copy of bit rotation routines" -- recommends including "bitops.h" and using its rol32 and ror32 rather than copy-and-paste into a new program.
  4. T. Oshiba, "Closure property of the family of context-free languages under the cyclic shift operation", Transactions of IECE, 55D:119–122, 1972.
  5. A. N. Maslov, "Cyclic shift operation for languages", Problems of Information Transmission 9:333–338, 1973.
  6. {{#invoke:Citation/CS1|citation |CitationClass=journal }}.