我正在做一个非常微不足道的计算机科学作业,围绕 C 中的位运算符展开,但我遇到了一个问题,我不知道它为什么会发生。这是我正在运行的代码。
#include <stdio.h>
int main(int argc, char *argv[]){
int bitPattern1 = 0x10011001;
int bitPattern2 = 0x01100110;
int bitPattern3 = 0xFFFFFFFF;
int bitPattern4 = 0x00000000;
/* pair 1 */
printf("bitPattern1 & bitPattern2 = %x\n", bitPattern1 & bitPattern2);
printf("bitPattern1 && bitPattern2 = %x\n", bitPattern1 && bitPattern2);
/* pair 2 */
printf("bitPattern1 | bitPattern2 = %x\n", bitPattern1 | bitPattern2);
printf("bitPattern1 || bitPattern2 = %x\n", bitPattern1 || bitPattern2);
/* pair 3 */
printf("bitPattern1 & bitPattern3 = %x\n", bitPattern1 & bitPattern3);
printf("bitPattern1 && bitPattern3 = %x\n", bitPattern1 && bitPattern3);
/* pair 4 */
printf("bitPattern1 | bitPattern3 = %x\n", bitPattern1 | bitPattern3);
printf("bitPattern1 || bitPattern3 = %x\n", bitPattern1 || bitPattern3);
/* pair 5 */
printf("bitPattern2 & bitPattern4 = %x\n", bitPattern2 & bitPattern4);
printf("bitPattern2 && bitPattern4 = %x\n", bitPattern2 && bitPattern4);
/* pair 6 */
printf("bitPattern2 | bitPattern4 = %x\n", bitPattern2 | bitPattern4);
printf("bitPattern2 || bitPattern4 = %x\n", bitPattern2 || bitPattern4);
return 0;
}
问题发生在第 4 对的第一部分,它返回以下结果:
bitPattern1 | bitPattern3 = ffffffff
bitPattern1 || bitPattern3 = 1
为什么按位运算符返回“f”我查看了文档,虽然 OR 运算符返回 1 或零,但该示例似乎应该返回“11111111”。它与十六进制数字有关吗?我一直无法在谷歌上找到任何东西。