C Bitwise AND Assignment Operator
In C, the Bitwise AND Assignment &=
operator is a compound assignment operator that performs a bitwise AND operation on two values and assigns the result to the left operand.
Bitwise AND Assignment operator is commonly used in low-level programming, bit masking, and flag manipulation.
Syntax of the Bitwise AND Assignment Operator
The syntax to use the Bitwise AND Assignment operator is:
variable1 &= variable2;
Explanation:
variable1
: The left operand whose value is modified.&=
: The Bitwise AND Assignment operator.variable2
: The right operand used for the bitwise AND operation.- The result of
variable1 & variable2
is computed and stored back invariable1
.
Examples of the Bitwise AND Assignment Operator
1. Basic Usage of Bitwise AND Assignment Operator
In this example, we will apply the &=
operator on two integer values and observe the result.
main.c
#include <stdio.h>
int main() {
int a = 6; // Binary: 0110
int b = 3; // Binary: 0011
a &= b; // Perform bitwise AND and assign the result to a
printf("Result after Bitwise AND Assignment: %d\n", a);
return 0;
}
Explanation:
- We declare two integer variables
a
andb
with values 6 and 3. - The binary representation of 6 is
0110
, and 3 is0011
. - The bitwise AND operation
0110 & 0011
results in0010
(decimal 2). - The result is stored in
a
, updating its value to 2.
Output:
Result after Bitwise AND Assignment: 2
2. Using Bitwise AND Assignment for Even Number Check
In this example, we use the &=
operator to determine if a number is even by performing a bitwise AND with 1.
main.c
#include <stdio.h>
int main() {
int num = 10; // Binary: 1010
num &= 1; // Perform bitwise AND with 1
if (num == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
Explanation:
- We declare an integer variable
num
with the value 10. - The binary representation of 10 is
1010
. - We perform
num &= 1
, which results in1010 & 0001
(binary), giving0000
(decimal 0). - Since the result is 0, we print “The number is even.”
Output:
The number is even.
3. Bitwise AND Assignment for Flag Manipulation
In this example, we use the &=
operator to turn off a specific bit in a number.
main.c
#include <stdio.h>
int main() {
int flags = 7; // Binary: 0111
int mask = ~2; // Mask to turn off the second bit (Binary: 1111 1101)
flags &= mask; // Apply bitwise AND assignment
printf("Flags after masking: %d\n", flags);
return 0;
}
Explanation:
- We declare an integer
flags
with the value 7 (binary0111
). - We create a mask
~2
, which inverts2
(binary0010
) to1111 1101
. - Using
flags &= mask
, the second bit inflags
is turned off. - The result is stored in
flags
and printed.
Output:
Flags after masking: 5
Conclusion
In this tutorial, we covered the Bitwise AND Assignment &=
operator in C:
- The
&=
operator performs a bitwise AND operation and assigns the result to the left operand. - It is commonly used in bit manipulation, flag management, and masking operations.
- The return value is the updated value of the left operand.