Is the compound operator '&=' logical or bitwise AND ?
In other words, is a &= b
the same as:
a = a & b
a = a && b
Is the compound operator '&=' logical or bitwise AND ?
In other words, is a &= b
the same as:
a = a & b
a = a && b
a &= b
is using the bitwise AND operator. Think of the +=
operation:
a += 5;
is the same as:
a = a + 5;
It's just a combination of two operations: &
and =
.
In C, a &= b
is a = a & b
, i.e. bitwise. In C++, where there is a dedicated bool
type, &=
on booleans is boolean as well, as is a simple &
on bool
. None of these does exhibit the short-circuit behaviour of &&
, though.
It's bitwise AND
simple
When you do a&=b
It means a=a&b
Remember a
and b
should be integral type
or promoted to integer type
While &&
is logical AND
.
This is one of the queries that can be resolved through experimentation rather than interrogation:
#include <stdio.h>
#include <inttypes.h>
int main(int argc, char *argv[]) {
uint8_t a = 0xFF;
uint8_t b = 0x0F;
a &= b;
printf("a &= b : %02X\n",a);
a = 0xFF;
printf("a & b : %02X\n", a & b);
printf("a && b: %02X\n", a && b);
}
Prints:
a &= b : 0F
a & b : 0F
a && b: 01
to the console.