Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/cprogs/Ex_2.8_rightrot.c
1240 views
1
/* write a function rightrot(x,n) that returns the value of the integer x rotated to rightby n bit positions */
2
3
#include<stdio.h>
4
5
unsigned rightrot(unsigned x,int n);
6
7
int main(void)
8
{
9
printf("%u",(unsigned)rightrot((unsigned)8,(int)1));
10
11
return 0;
12
}
13
14
/* rightrot: rotate x to right by n bit positions */
15
16
unsigned rightrot(unsigned x,int n)
17
{
18
int wordlength(void);
19
unsigned rbit;/* rightmost bit */
20
21
rbit = x << (wordlength() - n);
22
x = x >> n;
23
x = x | rbit;
24
25
return x;
26
}
27
28
int wordlength(void)
29
{
30
int i;
31
unsigned v = (unsigned) ~0;
32
33
for(i=1;(v=v>>1)>0;i++)
34
;
35
return i;
36
}
37
38
39
40