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:

    The night in prison was novel and interesting enough.... I found that even here there was a history and a gossip which never circulated beyond the walls of the jail. Probably this is the only house in the town where verses are composed, which are afterward printed in a circular form, but not published. I was shown quite a long list of verses which were composed by some young men who had been detected in an attempt to escape, who avenged themselves by singing them.
    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)