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:
“A thing is called by a certain name because it instantiates a certain universal is obviously circular when particularized, but it looks imposing when left in this general form. And it looks imposing in this general form largely because of the inveterate philosophical habit of treating the shadows cast by words and sentences as if they were separately identifiable. Universals, like facts and propositions, are such shadows.”
—David Pears (b. 1921)
“God is a foreman with certain definite views
Who orders life in shifts of work and leisure.”
—Seamus Heaney (b. 1939)