-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path2-8.c
47 lines (36 loc) · 831 Bytes
/
2-8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Exercise 2-8. Write a function rightrot(x,n) that returns the value of the
* integer x rotated to right by n positions.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
/* functions */
unsigned rightrot(unsigned, int);
size_t int_size();
/* int_size: determine the size of an integer storged on this machine */
size_t int_size()
{
int x = ~0;
size_t i = 0;
while (x != 0) {
x <<= 1;
++i;
}
return i;
}
unsigned rightrot(unsigned x, int n)
{
unsigned size, bits;
size = int_size(); /* never assume size of an int */
n = n % size; /* scale down the shift count to a defined range */
bits = x & ~(~0 << n); /* extract the n bits to be rotated */
return (x >> n) | bits << (size - n);
}
int main(void)
{
unsigned x = 0XAF14E5CB;
int n = 8;
printf("%x\n", rightrot(x, n));
return 0;
}