Closed. This question is off-topic. It is not currently accepting answers. Learn more
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
5年前关闭。
我正在使用termcaps,我不明白在这个例子中&=意味着什么:
term.c_lflag &= ~(ICANON);

有人能给我解释一下这是怎么回事吗?

最佳答案

这是将表示位字段的整数中的特定位设置为0的常见方法。

unsigned a = ...;
// ...
unsigned int mask = 1 << 11;  // mask for 12th bit
a |=  mask;  // set 12th bit to 1
a &= ~mask;  // set 12th bit to 0

启用位的方法是按位-or使用一个看起来像000010000的掩码的数字。
禁用位的工作方式是按位-and使用类似于111101111的掩码的数字(因此需要~,它表示按位求反)。
请注意,还有其他用于管理位字段的选项:
在C++中,使用std::bitset或甚至std::vector<bool>
在C或C++中,使用位字段结构
struct Foo {
   int foo_enabled : 1;
   int bar_enabled : 1;
   // ...
};

10-05 18:16