Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 18, 2015 11:11
Show Gist options
  • Select an option

  • Save tomcha/c1ce537fbf8be38adede to your computer and use it in GitHub Desktop.

Select an option

Save tomcha/c1ce537fbf8be38adede to your computer and use it in GitHub Desktop.
2-8
#include <stdio.h>
int rightrot(unsigned x, int n);
int main(){
int x = 0b111010;
printf("%d\n",rightrot(x, 2));
// 111010 -> 101110 = 46
}
int rightrot(unsigned x, int n){
int i = 0;
int nx = x;
while(nx > 0){
nx /= 2;
i++;
}
// printf("i:%d\n",i);
if(i < n){
printf("桁数が足りません\n");
return -1;
}else{
//下n桁の数字をとる
int shiftbit = x & ~(~0 << n);
// printf("sb:%d\n", shiftbit);
//n桁右シフト
x = x >> n;
// printf("shifted_x:%d\n", x);
//下n桁をnumber - n 左シフト
shiftbit = shiftbit << (i - n);
//足す
return x + shiftbit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment