C将int转换为bitshift运算符

Mur*_*uru -1 c logic bit-manipulation

我只能使用这些符号:

!〜&^ | + << >>

这是我需要实现的表:

input | output
--------------
0     |   0
1     |   8
2     |   16
3     |   24
Run Code Online (Sandbox Code Playgroud)

随着输出我将左移32位int.

防爆.

int main()
{
   int myInt = 0xFFFFFFFF;
   myInt = (x << (myFunction(2)));

  //OUTPUT = 0xFFFF0000
}

int myFunction(int input)
{ 
   // Do some magic conversions here
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗????

Mih*_*eac 7

好吧,如果你想用的功能f(0) = 0,f(1) = 8,f(3) = 24等等,那么你就必须实现f(x) = x * 8.由于8是2的完美幂,乘法可以通过移位来代替.从而:

int myFunction(int input)
{
    return input << 3;
}
Run Code Online (Sandbox Code Playgroud)

就这样.