Hel*_*len 4 c byte bit-manipulation bit
我有:
int8_t byteFlag;
我想得到它的第一部分?我想我可能需要使用&和>>准确,但不知道如何.有帮助吗?
int func(int8_t byteFlag, int whichBit)
{
if (whichBit > 0 && whichBit <= 8)
return (byteFlag & (1<<(whichBit-1)));
else
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在func(byteFlag, 1)将从LSB返回第1位.您可以通过8为whichBit获得第8位(MSB).
<<是一个左移操作员.它会将值移动1到适当的位置,然后我们必须进行&操作以获得该特定位的值byteFlag.
对于 func(75, 4)
75 -> 0100 1011
1 -> 0000 0001
1 << (4-1) -> 0000 1000 //means 3 times shifting left
Run Code Online (Sandbox Code Playgroud)
75 & (1 << (4 - 1))会给我们1.