HOME

TheInfoList



OR:

In combinatorial mathematics, a circular shift is the operation of rearranging the entries in a
tuple In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
, 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 In mathematics, a permutation of a set is, loosely speaking, an arrangement of its members into a sequence or linear order, or if the set is already ordered, a rearrangement of its elements. The word "permutation" also refers to the act or p ...
. Formally, a circular shift is a
permutation In mathematics, a permutation of a set is, loosely speaking, an arrangement of its members into a sequence or linear order, or if the set is already ordered, a rearrangement of its elements. The word "permutation" also refers to the act or p ...
σ of the ''n'' entries in the tuple such that either :\sigma(i)\equiv (i+1)
modulo In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the '' modulus'' of the operation). Given two positive numbers and , modulo (often abbreviated as ) is t ...
''n'', for all entries ''i'' = 1, ..., ''n'' or :\sigma(i)\equiv (i-1)
modulo In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the '' modulus'' of the operation). Given two positive numbers and , modulo (often abbreviated as ) is t ...
''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 In mathematics, a divisor of an integer n, also called a factor of n, is an integer m that may be multiplied by some integer to produce n. In this case, one also says that n is a multiple of m. An integer n is divisible or evenly divisible by ...
of ''n'', depending on the entries of the tuple. In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, 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 Exponentiation is a mathematical operation, written as , involving two numbers, the '' base'' and the ''exponent'' or ''power'' , and pronounced as " (raised) to the (power of) ". When is a positive integer, exponentiation corresponds to re ...
from its
significand The significand (also mantissa or coefficient, sometimes also argument, or ambiguously fraction or characteristic) is part of a number in scientific notation or in floating-point representation, consisting of its significant digits. Depending on ...
. Unlike a
logical shift In computer science, a logical shift is a bitwise operation that shifts all the bits of its operand. The two base variants are the logical left shift and the logical right shift. This is further modulated by the number of bit positions a giv ...
, 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 Cryptography, or cryptology (from grc, , translit=kryptós "hidden, secret"; and ''graphein'', "to write", or ''-logia'', "study", respectively), is the practice and study of techniques for secure communication in the presence of adve ...
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 In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic ope ...
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 (&), prevents undefined behaviour * when the shift count is 0 or >= the width of unsigned int. */ #include // for uint32_t, to get 32-bit-wide rotates, regardless of the size of int. #include // for CHAR_BIT uint32_t rotl32 (uint32_t value, unsigned int count) uint32_t rotr32 (uint32_t value, unsigned int count) 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 count is limited to the range of 1 to 31 bits: uint32_t rotl32 (uint32_t value, unsigned int count) 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.


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 String or strings may refer to: *String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects Arts, entertainment, and media Films * ''Strings'' (1991 film), a Canadian anim ...
''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 In logic, mathematics, computer science, and linguistics, a formal language consists of words whose letters are taken from an alphabet and are well-formed according to a specific set of rules. The alphabet of a formal language consists of sy ...
. 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 A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
of length ''n'', there is a regular expression of length O(''n''3) describing ''shift''(''L'')..


See also

* Barrel shifter * Circulant * Lyndon word *
Necklace A necklace is an article of jewellery that is worn around the neck. Necklaces may have been one of the earliest types of adornment worn by humans. They often serve ceremonial, religious, magical, or funerary purposes and are also used as symb ...
— an object like a
tuple In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
but for which circular shifts are considered equivalent.


References

{{DEFAULTSORT:Circular Shift Elementary mathematics Computer arithmetic