当无符号字符与 11100000 进行 OR 运算时,为什么按位 OR 运算无法按预期工作?

use*_*886 0 c bit-manipulation bit-shift bitwise-operators bitwise-or

我无法理解为什么操作 'c | 11100000' 似乎不起作用。但我也注意到 'c | 10000000' 按预期工作。

#include <stdio.h>

int main()
{
    unsigned char c, c1;
    
    c = c & 0;
    c = c | 11100000;
    printf("%o \t", c);
    
    /** prints 140 ***/
    
    
    c = c & 0;
    c = c | 111;
    c << 5;
    printf("%o", c);
    
    /** prints 157 **/

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

dbu*_*ush 5

您使用的常量值是十进制格式,而不是二进制格式。

C 不支持二进制常量,但支持十六进制常量:

c = c | 0xe0;
...
c = c | 0x7;
Run Code Online (Sandbox Code Playgroud)

另外,这不会执行任何操作:

c << 5;
Run Code Online (Sandbox Code Playgroud)

想必,您想要:

c = c << 5;
Run Code Online (Sandbox Code Playgroud)