[c]代码库
/* 循环移位 */
#include <stdio.h>
void print_bin(const int a, const int size) {
unsigned flag = 1 << (size * 8 - 1);
while (flag != 0) {
printf("%d", 0 != (a & flag));
flag >>= 1;
}
}
unsigned cycclic_right_shift(unsigned a, int n) {
unsigned b = 0;
int size = sizeof(unsigned) * 8;
n %= size;
b = a << (size - n);
a = a >> n;
a |= b;
return a;
}
int main(void) {
int size = 0;
int a = 0;
int n = 0;
printf("a = ");
scanf("%d", &a);
printf("n = ");
scanf("%d", &n);
print_bin(a, sizeof(unsigned));
printf("\n");
a = cycclic_right_shift(a, n);
print_bin(a, sizeof(unsigned));
printf("\n");
return 0;
}
by: 发表于:2017-08-21 14:32:30 顶(0) | 踩(0) 回复
??
回复评论