Sva*_*ana 10 c++ bit-manipulation visual-c++
我想比特移位一个变量并存储一个在布尔中移出的位.
就像是:
unsigned int i = 1;
bool b = rshift(&i); // i now equals 0 and b is set to true
Run Code Online (Sandbox Code Playgroud)
如何实现这一目标?
NPE*_*NPE 14
您必须在班次之前捕获该位:
bool rshift(unsigned int* p) {
bool ret = (*p) & 1;
*p >>= 1;
return ret;
}
Run Code Online (Sandbox Code Playgroud)
你没有.你必须在班次之前测试它:
bool
rshift( unsigned& i )
{
bool results = (i & 0x01) != 0;
i >>= 1;
return results;
}
Run Code Online (Sandbox Code Playgroud)
(这对于右移是很简单的.对于左移,你必须知道单词中的位数.)