Bitwise operators are used to process numbers with respect to their binary representation. The main task of bitwise operators is to manipulate binary values bitwise.
The operations are directly supported by the processor. Bitwise operations are required for working with device drivers, low-level graphics, cryptography, and network communication.
Operator | Description |
---|---|
~ | Bit complement |
<< | Left shift of bit patterns |
>> | Right shift of bit patterns |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
Line | Description |
---|---|
5 | Declares the integer variables x, y, z, a, b, c .x , y and z are not initialized. The variable a is initialized with the value 7, b with the value 12 and c with the value 15. |
6 | Bitwise AND linking of the two variables a and b |
7 | Bitwise OR linking of the two variables a and b |
8 | Bitwise XOR linking of the two variables a and b |
9 – 11 | Outputs the result of the bit operators |
#include <iostream>
using namespace std;
int main() {
int x, y, z, a=7, b=12, c=15;
x = a & b;
z = a | b;
y = a ^ c;
cout << "Bitwise AND: " << x << endl;
cout << "Bitwise OR: " << z << endl;
cout << "Bitwise XOR: " << y << endl;
}