Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created October 30, 2009 11:39
Show Gist options
  • Save shaobin0604/222279 to your computer and use it in GitHub Desktop.
Save shaobin0604/222279 to your computer and use it in GitHub Desktop.
/*
* Exercise 2-8. Write a function rightrot(x,n) that returns the value of the
* integer x rotated to the right by n positions.
*/
#include <stdio.h>
unsigned int rightrot(unsigned int x, int n);
unsigned int rightrot(unsigned int x, int n) {
while (n > 0) {
if ((x & 1) == 1)
x = (x >> 1) | ~(~0u >> 1);
else
x = x >> 1;
n--;
}
return x;
}
int main(void)
{
unsigned int x = 0x000000f8;
unsigned int r = 0xc0000007;
int n = 5;
if (r == rightrot(x, n))
printf("assert sucessful!\n");
else
printf("assert fail!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment