Created
October 30, 2009 11:39
-
-
Save shaobin0604/222279 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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