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:

</>
Copy
variable1 &= variable2;

Explanation:

  1. variable1: The left operand whose value is modified.
  2. &=: The Bitwise AND Assignment operator.
  3. variable2: The right operand used for the bitwise AND operation.
  4. The result of variable1 & variable2 is computed and stored back in variable1.

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

</>
Copy
#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:

  1. We declare two integer variables a and b with values 6 and 3.
  2. The binary representation of 6 is 0110, and 3 is 0011.
  3. The bitwise AND operation 0110 & 0011 results in 0010 (decimal 2).
  4. 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

</>
Copy
#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:

  1. We declare an integer variable num with the value 10.
  2. The binary representation of 10 is 1010.
  3. We perform num &= 1, which results in 1010 & 0001 (binary), giving 0000 (decimal 0).
  4. 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

</>
Copy
#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:

  1. We declare an integer flags with the value 7 (binary 0111).
  2. We create a mask ~2, which inverts 2 (binary 0010) to 1111 1101.
  3. Using flags &= mask, the second bit in flags is turned off.
  4. 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:

  1. The &= operator performs a bitwise AND operation and assigns the result to the left operand.
  2. It is commonly used in bit manipulation, flag management, and masking operations.
  3. The return value is the updated value of the left operand.