In this tutorial, you shall learn about Bitwise AND Operator in C++ programming language, its syntax, and how to use this operator with the help of examples.
C++ Bitwise AND
C++ Bitwise AND Operator is used to perform AND operation between the respective bits of given operands.
Syntax
The syntax for Bitwise AND operation between x
and y
operands is
x & y
The operands can be of type int
or char
. Bitwise AND operator returns a value of type same as that of the given operands.
Truth Table
The following table illustrates the output of AND operation between two bits.
bit1 | bit2 | bit1 & bit2 |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Examples
1. Bitwise AND between two integer values
In the following example, we take integer values in x
and y
, and find the bitwise AND operation between x
and y
.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 9;
int result = x & y;
cout << "Result : " << result << endl;
}
Output
Result : 1
Program ended with exit code: 0
2. Bitwise AND between two char values
In the following example, we take char values in x
and y
, and find the bitwise AND operation between x
and y
.
main.cpp
#include <iostream>
using namespace std;
int main() {
char x = 'A';
char y = 'B';
char result = x & y;
cout << "Result : " << result << endl;
}
Output
Result : @
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned what Bitwise AND Operator is, its syntax, and how to use this operator in C++ programs, with the help of examples.