Bitwise operations: Difference between revisions

(→‎{{header|AWK}}: gawk has compl(n) for bitwise not. OpenBSD awk also has these functions.)
Line 255:
return 0;
}</lang>
 
To rotate an integer, you can combine a left shift and a right shift:
<lang C>/* rotate x to the right by s bits */
unsigned int rotr(unsigned int x, unsigned int s)
{
return (x >> s) | (x << 32 - s);
}</lang>With a smart enough compiler, the above actually compiles into a single machine bit rotate instruction when possible. E.g. <code>gcc -S</code> on IA32 produced following assembly code:<lang Assembly>rotr:
movl 4(%esp), %eax ; arg1: x
movl 8(%esp), %ecx ; arg2: s
rorl %cl, %eax ; right rotate x by s
ret</lang>
 
=={{header|C++}}==
Anonymous user