Circular shifts: Mathematical concept and applications in software development
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 
formula_1 modulo "n", for all entries "i" = 1, ..., "n"
or
formula_2 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
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. The number of distinct circular shifts of an "n"-tuple is formula_3, where k is a divisor of n, indicating the maximal number of repeats over all subpatterns.
In computer programming, a bitwise rotation, also known as a circular shift, is a bitwise operation 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 floating-point number's exponent from its significand. 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, 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.
/*
 * Shift operations in C are only defined for shift values which are
 * not negative and smaller than sizeof(value) * CHAR_BIT.
 * The mask, used with bitwise-and (&amp;), prevents undefined behaviour
 * when the shift count is 0 or ;= the width of unsigned int.
uint32_t rotl32 (uint32_t value, unsigned int count) {
 const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
 count &amp;= mask;
 return (value « count) | (value » (-count &amp; mask));
uint32_t rotr32 (uint32_t value, unsigned int count) {
 const unsigned int mask = CHAR_BIT * sizeof(value) - 1;
 count &amp;= mask;
 return (value » count) | (value « (-count &amp; mask));
This safe and compiler-friendly implementation was developed by John Regehr, and further polished by Peter Cordes.
A simpler version is often seen when the codice_1 is limited to the range of 1 to 31 bits:
uint32_t rotl32 (uint32_t value, unsigned int count) {
 return (value « count) | (value » (32 - count));
This version is dangerous because if the codice_1 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 codice_3 as either a 32-bit shift (producing 0) or a 0-bit shift (producing the original codice_4), and either one produces the correct result in this application.
Example.
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:
Applications.
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. Also, if "L" is described by a regular expression of length "n", there is a regular expression of length O("n"3) describing "shift"("L").
References.
 ;templatestyles src="Reflist/styles.css" / ;