C++Assignment OperatorsBitwise AND and assign

&= (Bitwise AND and Assign) Operator in C++

In C++, the &= operator is a compound bitwise assignment operator. It performs a bitwise AND operation between two operands and assigns the result to the left-hand operand.

It is a shorthand for writing: a = a & b;

This operator is primarily used with integer types and bit flags. It is useful when working with binary data, permissions, masks, or low-level operations.


Syntax

a &= b;  // Equivalent to: a = a & b;

This applies a bitwise AND operation and stores the result in a.


How Bitwise AND Works

Bitwise AND compares each bit of the two numbers. The result bit is 1 if both bits are 1, otherwise it is 0.

Example:
a = 6   →  0110 (in binary)
b = 3   →  0011 (in binary)
Result  →  0010 (which is 2 in decimal)

Example with Integers

bitwise_and_integers.cpp
#include <iostream>
int main() {
    int a = 6;
    int b = 3;
    a &= b;
    std::cout << "a = " << a << std::endl;  // Output: 2
    return 0;
}

Explanation:

  • 6 in binary: 0110
  • 3 in binary: 0011
  • Bitwise AND: 00102

Example with Binary Display

bitwise_and_binary.cpp
#include <iostream>
#include <bitset>
int main() {
    int a = 12;  // 1100
    int b = 10;  // 1010
    a &= b;
    std::cout << "Result = " << a << std::endl;
    std::cout << "Binary = " << std::bitset<4>(a) << std::endl;  // Output: 1000
    return 0;
}

Explanation:

  • 1100 & 1010 = 1000 (8 in decimal)
  • We use std::bitset to print the binary representation.

Example with char Type

bitwise_and_char.cpp
#include <iostream>
int main() {
    char ch = 0xF;  // 00001111
    ch &= 0xA;      // 00001010
    std::cout << "ch = " << (int)ch << std::endl;  // Output: 10
    return 0;
}

Explanation:

  • 00001111 & 00001010 = 0000101010

Example with Unsigned Integers

bitwise_and_unsigned.cpp
#include <iostream>
int main() {
    unsigned int x = 255;   // 11111111
    unsigned int y = 15;    // 00001111
    x &= y;
    std::cout << "x = " << x << std::endl;  // Output: 15
    return 0;
}

Explanation:

  • Keeps only the lowest 4 bits.

Example with Bit Masking (Flags)

bitwise_and_flags.cpp
#include <iostream>
#define READ  0x01  // 0001
#define WRITE 0x02  // 0010
#define EXEC  0x04  // 0100
 
int main() {
    int permissions = READ | WRITE;  // 0011
    permissions &= WRITE;
    std::cout << "WRITE allowed? " << (permissions ? "Yes" : "No") << std::endl;  // Output: Yes
    return 0;
}

Explanation:

  • Checks if WRITE permission is set using bitwise AND.

Example with Arrays (Element-wise)

bitwise_and_arrays.cpp
#include <iostream>
int main() {
    int a[3] = {7, 12, 5};
    int b[3] = {3, 10, 6};
    for (int i = 0; i < 3; i++) {
        a[i] &= b[i];
    }
    for (int i = 0; i < 3; i++) {
        std::cout << "a[" << i << "] = " << a[i] << std::endl;
    }
    return 0;
}

Explanation:

  • Each element in a is updated with the bitwise AND of the corresponding element in b.

Example with Pointers

bitwise_and_pointer.cpp
#include <iostream>
int main() {
    int x = 14;  // 1110
    int* ptr = &x;
    *ptr &= 9;   // 1001
    std::cout << "x = " << x << std::endl;  // Output: 8
    return 0;
}

Example with Custom Types (Operator Overloading)

custom_operator_and.cpp
#include <iostream>
class Flag {
    int bits;
public:
    Flag(int b) : bits(b) {}
    Flag& operator&=(int mask) {
        bits &= mask;
        return *this;
    }
    void show() {
        std::cout << "Bits = " << bits << std::endl;
    }
};
 
int main() {
    Flag f(7);  // 0111
    f &= 5;     // 0101
    f.show();   // Output: Bits = 5
    return 0;
}

Not Applicable for These Types

  • std::string – Not supported
  • float, double – Not supported
  • bool – Valid but not meaningful beyond 0 or 1
bitwise_and_string_error.cpp
// std::string s1 = "A", s2 = "B";
// s1 &= s2;  // ❌ Error: invalid operands

Summary Table

Data TypeSupportedExampleNotes
intYesa &= bCommon and widely supported
charYesch &= maskUseful for low-level bit operations
unsigned intYesx &= yUsed for masking
float, doubleNof &= g❌ Not supported
std::stringNos1 &= s2❌ Not supported
ArraysIndirectLoop over elementsUse loop for element-wise operation
PointersIndirect*ptr &= maskApply to pointed value
Custom ClassYesOverload &=Can define behavior via operator overloading

Conclusion

The &= operator is a powerful tool when working with bits and binary data in C++. It is essential in scenarios like permissions, flags, hardware interfacing, and performance-critical applications.

For beginners, understanding how the operator works with binary numbers will deepen your grasp of low-level data manipulation in C++. Avoid using it with non-integer types and always validate logic when using masks or flags.

Last updated on