What does &= mean?

c, termcap

Solution

That's a common way to set a specific bit to 0 in an integer that represents a bitfield.

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

Enabling a bit works by bitwise-`or`ing a number with a mask that looks like `000010000`. Disabling a bit works by bitwise-`and`ing a number with a mask like `111101111` (hence the need for `~`, which stands for bitwise negation).

Note that there are also other options for managing bitfields:

- in C++, using `std::bitset` or even `std::vector<bool>`

in C or C++, using a bitfield struct like

struct Foo {
   int foo_enabled : 1;
   int bar_enabled : 1;
   // ...
};

Problem

I'm using termcaps and I don't understand what `&=` means in this example: ``` term.c_lflag &= ~(ICANON); ``` Could anyone explain to me how this works?

Original source