c语言 7-3
1、
#include <stdio.h>
unsigned rrotate(unsigned x, int n)
{
return x >> n;
}
unsigned lrotate(unsigned x, int n)
{
return x << n;
}
int main(void)
{
unsigned x; int n;
puts("please input the test number and move bits.");
printf("x = "); scanf("%u", &x);
printf("n = "); scanf("%u", &n);
printf("right : %u | left : %u\n", rrotate(x, n), lrotate(x, n));
return 0;
}
2、
#include <stdio.h> unsigned rrotate(unsigned x, int n) { return x >> n; } unsigned lrotate(unsigned x, int n) { return x << n; } int main(void) { unsigned x; int n; puts("please input the test number and move bits."); printf("x = "); scanf("%u", &x); printf("n = "); scanf("%d", &n); int i; for(;;) { puts("choose: 1 →right;2 →left"); printf("i = "); scanf("%d", &i); if(i == 1 | i == 2) break; puts("input error, 1 or 2 ?"); } if(i == 1) printf("right: %u\n", rrotate(x, n)); else printf("left : %u\n", lrotate(x, n)); return 0; }
3、
#include <stdio.h> unsigned rrotate(unsigned x, int n) { return x >> n; } unsigned lrotate(unsigned x, int n) { return x << n; } int main(void) { unsigned x; int n; puts("please input the test number and move bits."); printf("x = "); scanf("%u", &x); printf("n = "); scanf("%d", &n); int i; while(!0) { puts("choose: 1 →right; 2 →left"); printf("i = "); scanf("%d", &i); if(i == 1 || i == 2) break; puts("input error! 1 or 2 ?"); } if(i == 1) printf("right: %u\n", rrotate(x, n)); else printf("left : %u\n", lrotate(x, n)); return 0; }
4、
#include <stdio.h> unsigned rrotate(unsigned x, int n) { return x >> n; } unsigned lrotate(unsigned x, int n) { return x << n; } int main(void) { unsigned x; int n; puts("please input the test number and move bits."); printf("x = "); scanf("%u", &x); printf("n = "); scanf("%d", &n); int i; do { puts("choose i: 1 →right; 2 →left"); printf("i = "); scanf("%d", &i); if(i < 1 || i > 2) puts("input error! 1 or 2 ?"); } while(i < 1 || i > 2); if(i == 1) printf("right: %u\n", rrotate(x, n)); else printf("left : %u\n", lrotate(x, n)); return 0; }