我只能使用这些符号:
! 〜& ^ | + << >>
这是我需要实现的表格:
input | output
--------------
0 | 0
1 | 8
2 | 16
3 | 24
对于输出,我将左移一个 32 位 int。
例如。
int main()
{
int myInt = 0xFFFFFFFF;
myInt = (x << (myFunction(2)));
//OUTPUT = 0xFFFF0000
}
int myFunction(int input)
{
// Do some magic conversions here
}
有什么想法吗???
最佳答案
好吧,如果您想要一个具有 f(0) = 0
、f(1) = 8
、f(3) = 24
的函数code> 等等,那么您必须实现 f(x) = x * 8
。由于 8 是 2 的完美幂,因此可以用移位代替乘法。因此:
int myFunction(int input)
{
return input << 3;
}
仅此而已。
关于C 将 int 转换为位移运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7259298/