C++ 'and_eq' Keyword with Example

C++ | 'and_eq' keyword: Here, we are going to learn about the 'and_eq' keyword which is an alternative to Bitwise AND Assignment operator.
Submitted by IncludeHelp, on May 16, 2020

"and_eq" is an inbuilt keyword that has been around since at least C++98. It is an alternative to &= (Bitwise AND Assignment) operator and it mostly uses for bit manipulations.

The and_eq keyword performs Bitwise AND operation and assigns the result to the left/first operand.

Syntax:

    operand_1 and_eq operand_2;

Here, operand_1 and operand_2 are the operands.

Example:

    Input:
    bitset<4> value("1100");
    bitset<4> mask ("1010");
        
    value and_eq mask;

    Output:
    value = 1000

C++ example to demonstrate the use of "and_eq" keyword

// C++ example to demonstrate the use of
// 'and_eq' keyword

#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    //bitsets
    bitset<4> value("1011");
    bitset<4> mask1("1100");
    bitset<4> mask2("0100");

    // before operation
    cout << "value: " << value << endl;
    cout << "mask1: " << mask1 << endl;
    cout << "mask2: " << mask2 << endl;

    value and_eq mask1;

    cout << "After operation (1)...\n";
    cout << "value: " << value << endl;

    value and_eq mask2;

    cout << "After operation (2)...\n";
    cout << "value: " << value << endl;

    return 0;
}

Output:

value: 1011
mask1: 1100
mask2: 0100
After operation (1)...
value: 1000
After operation (2)...
value: 0000

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.