Ale*_*ews 2 c bit-manipulation bitwise-or
我试图将char中的所有位设置为true.
char foo = 00000000;
foo |= 11111111;
for (int i = 0; i < 8; i++) { //prints out bitwise
printf("%d", !!((foo << i) & 0x80));
}
Run Code Online (Sandbox Code Playgroud)
当foo全为0时,我得到11000111.当foo == 00000110时,它打印11001111; 这里出了什么问题?
该数字11111111是十进制常量,而不是二进制.虽然您可以使用八进制或十六进制常量,但没有二进制常量(至少不是标准常量).
如果你想要一个设置了所有位的数字,只需将按位补码运算符~应用于0:
unsigned char foo = ~0u;
Run Code Online (Sandbox Code Playgroud)