The technique of applying logical operations on a series of bits to get the desired result is known as bit manipulation. Here is a list of C++ questions on bit manipulation.
How to use bitwise AND operators?
#include <iostream>
using namespace std;
int main() {
// First lets delare two variables.
int a = 56, b = 60;
/*
The binary form of a and b is,
a = 00111000
b = 00111100
*/
// To use bitwise AND operator
cout << "a AND b = " << (a & b) << endl;
/*
inside the program
a = 00111000
b = 00111100
----------------
a&b = 00111000
so the binary form of a AND b is 56.
*/
return 0;
}
Output
a AND b = 56
How to use bitwise OR operator?
#include <iostream>
using namespace std;
int main() {
// First lets delare two variables.
int a = 56, b = 60;
/*
The binary form of a and b is,
a = 00111000
b = 00111100
*/
// To use bitwise OR operator
cout << "a OR b = " << (a | b) << endl;
/*
inside the program
a = 00111000
b = 00111100
----------------
a|b = 00111100
so the binary form of a OR b is 60.
*/
return 0;
}
Output
a OR b = 60
How to use bitwise XOR operator?
#include <iostream>
using namespace std;
int main() {
// First lets delare two variables.
int a = 56, b = 60;
/*
The binary form of a and b is,
a = 00111000
b = 00111100
*/
// To use bitwise XOR operator
cout << "a XOR b = " << (a ^ b) << endl;
/*
inside the program
a = 00111000
b = 00111100
----------------
a^b = 00000100
so the binary form of a XOR b is 4.
*/
return 0;
}
Output
a XOR b = 4