Net*_*ave 1 c c++ bit-manipulation
我想知道char上不同位的状态
假设我说:
char a = 11 //00001011 in binary
Run Code Online (Sandbox Code Playgroud)
如何检索当前为0的位号5并将其转换为bool变量?我该怎么设置它?
您可以使用一些按位运算符:
bool fifthBitSet = a & 0x10;
Run Code Online (Sandbox Code Playgroud)
或者,更一般地说:
bool nThBitSet = a & ( 0x1 << n );
Run Code Online (Sandbox Code Playgroud)
错过了设置部分:
a |= 0x1 << n; // set n'th bit
Run Code Online (Sandbox Code Playgroud)