编写一函数用来实现左右循环移位。函数名为move,调用方法为move(value,n);其中value为要循环位移的数,n为位移的位数,如n < 0表示为左移; n > 0为右移;如n = 4表示要右移4位,n = -3表示要左移3位。
代码如下:
#include <stdio.h> unsigned short moveleft(unsigned short ,int); unsigned short moveright(unsigned short,int); int main(int argc, char *argv[]) { unsigned short a; int n; printf("input an octal number:"); scanf("%o",&a); printf("input n:"); scanf("%d",&n); if (n > 0) { printf("result:%o\n",moveright(a,n)); } else { n = -n; printf("result:%o\n",moveleft(a,n)); } system("pause"); } unsigned short moveleft(unsigned short value,int n) { unsigned short z; z = (value >> (16 - n)) | (value << n); return z; }
unsigned short moveright(unsigned short value ,int n) { unsigned short z; z = (value >> n) | (value << (16 - n)); return z; }
|
阅读(3799) | 评论(0) | 转发(0) |