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:
“Whoso desireth to know what will be hereafter, let him think of what is past, for the world hath ever been in a circular revolution; whatsoever is now, was heretofore; and things past or present, are no other than such as shall be again: Redit orbis in orbem.”
—Sir Walter Raleigh (15521618)
“God is a foreman with certain definite views
Who orders life in shifts of work and leisure.”
—Seamus Heaney (b. 1939)