What does &= mean?

43.7k Views Asked by At

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?

5

There are 5 best solutions below

0
On
term.c_lflag = (term.c_lflag) & (~ICANON)
0
On

It is not much different from the operator +=. Just like it term.c_lflag &= ~(ICANON); should have the effect of term.c_lflag = term.c_lflag & (~ICANON); but "in place". You will not create a temporary but instead you will modify term.c_lflag. Please note here & is the bitwise AND operator.

0
On

&= means Bit Wise AND and then assign. For example

term.c_lflag = (term.c_lflag) & (~(ICANON))

First, do term.c_lflag & ~(ICANON) then assign to term.c_lflag

0
On

The code turns off the ICANON bit, as Schwartz explain in comment: the ICANON defines a bit through a mask, e.g. 0x8000 (the ICANON bit is the one having value 1), when you apply the bitwise not operator, ~, all bits are "inverted" and you have (if the values are 16bit wide) 0x7FFF. If you put in bitwise and (&) this value with x, the result is to keep unchanged all bits of x matching a "1" bit in the mask 0x7FFF, and "turn off" the bits of x matching a "0" bit in the mask, which is exactly the ICANON bit in this specific case.

About x &= a, as already explained, it can be considered just syntactic sugar for x = x & a.

0
On

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-oring a number with a mask that looks like 000010000.
Disabling a bit works by bitwise-anding 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;
       // ...
    };