【发布时间】:2020-10-26 15:41:36
【问题描述】:
我在使用移位程序时遇到了一些问题。
挑战是编写一个程序,它可以将无符号整数向左移动数步。两个整数都由用户输入。因此,给定两个整数(
x和y),x中的位应向左移动y步,而左侧丢失的位应向右移动。即,在最高有效位之外丢失的位被放置在最低有效位。
为了解决这个挑战,我做了以下尝试:
#include <stdio.h>
#include <utility.h>
unsigned int bitshift(unsigned int a, unsigned int b)
{
a<<b;
b>>a;
return a,b;
}
int main (void)
{
unsigned int x, y;
printf("Enter two integers (smaller than 32) please:\n");
scanf("%u%u", &x, &y);
printf("Your integers are %u and %u.\n", x, y);
printf("In hexadecimal-format, your integers are %08x and %08x.\n", x, y);
printf("We are now going to perform a bit shift operation\n");
printf("The result of the bitshift operation is:\n");
printf("%u and %u\n", bitshift(x,y));
printf("In hexadecimal: %08x and %08x\n", bitshift(x,y));
while(!KeyHit());
return 0;
}
但是,我在编译时收到一条错误消息,例如“没有足够的参数”,我不明白。
但我最想知道的是bitshift 函数是否可以完成这项工作?
【问题讨论】:
-
return a, b;等价于return b;。见stackoverflow.com/questions/52550/… -
a<<b不会修改a。 -
错误信息是因为你的格式字符串有两个占位符,但你只给了一个值来格式化。
-
顺便说一句:更适合您的函数的名称是
rotatebits。 -
为什么你认为
bitshift()首先需要返回两个值?它应该只返回a的移位值,不需要更改b。
标签: c bit-manipulation bit