Circular Shift - Implementing Circular Shifts

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 several 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.

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". Rotates in the other direction may be "#define"ed as a macro or defined as an inline function called something like "leftrotate32" or "rotl32" in the same "bitops.h" header file.

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

unsigned int _rotl(const unsigned int value, int shift) { if ((shift &= sizeof(value)*8 - 1) == 0) return value; return (value << shift) | (value >> (sizeof(value)*8 - shift)); } unsigned int _rotr(const unsigned int value, int shift) { if ((shift &= sizeof(value)*8 - 1) == 0) return value; return (value >> shift) | (value << (sizeof(value)*8 - shift)); }

Read more about this topic:  Circular Shift

Famous quotes containing the words circular and/or shifts:

    If one doubts whether Grecian valor and patriotism are not a fiction of the poets, he may go to Athens and see still upon the walls of the temple of Minerva the circular marks made by the shields taken from the enemy in the Persian war, which were suspended there. We have not far to seek for living and unquestionable evidence. The very dust takes shape and confirms some story which we had read.
    Henry David Thoreau (1817–1862)

    The flattering, if arbitrary, label, First Lady of the Theatre, takes its toll. The demands are great, not only in energy but eventually in dramatic focus. It is difficult, if not impossible, for a star to occupy an inch of space without bursting seams, cramping everyone else’s style and unbalancing a play. No matter how self-effacing a famous player may be, he makes an entrance as a casual neighbor and the audience interest shifts to the house next door.
    Helen Hayes (1900–1993)