运营商&;=C中的~1U

运营商&;=C中的~1U,c,operators,C,Operators,如果s=8,这段代码做什么? 我假设它与二进制有关,但不知道具体是什么 提前谢谢 为了简单起见,我在这里用1字节二进制(8位)写出来 s = s & ~1 // U means "unsigned" s = 8 & ~(0b00000001) // Here is the binary representation of 1 s = 8 & 0b11111110 // ~1 is 254 s = 0b00001000 & 0b11111

如果
s=8
,这段代码做什么? 我假设它与二进制有关,但不知道具体是什么


提前谢谢

为了简单起见,我在这里用1字节二进制(8位)写出来

s = s & ~1            // U means "unsigned"
s = 8 & ~(0b00000001) // Here is the binary representation of 1
s = 8 & 0b11111110    // ~1 is 254
s = 0b00001000 & 0b11111110
s = 0b00001000

s == 8  // Final Answer.
这可以通过在上运行代码来确认

输入
取决于
s
的类型。Post其类型。该操作取消设置位0。因为它没有设置为8,所以它不会改变任何东西。
#include <stdio.h>
int main(void) {
    int s = 8;
    s &= ~1U;
    printf("%d\n", s);
    return 0;
}
Success #stdin #stdout 0s 9424KB
8