C++ Bitwise Operator Overloading
Last Updated :
27 Nov, 2022
Prerequisites:
In C++, there are a total of six bitwise operators. The six bitwise operators are bitwise AND (&), bitwise OR (|), bitwise XOR (^), left shift (<<), right shift (>>), and bitwise NOT (~).
- The & (bitwise AND) in C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
- The | (bitwise OR) in C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
- The ^ (bitwise XOR) in C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
- The << (left shift) in C++ takes two numbers, the left shifts the bits of the first operand, and the second operand decides the number of places to shift.
- The >> (right shift) in C++ takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of places to shift.
- The ~ (bitwise NOT) in C++ takes one number and inverts all bits of it.
Why Operator Overloading?
We can’t directly use the Bitwise Operator on objects. The simple explanation for this is that the Bitwise Operator is predefined to operate only on built-in Data types. As the class and objects are user-defined data types, so the compiler generates an error.
Example:
int a = 1, b=0;
int c= a & b;
cout<<c<<endl;
Output:
0
here, a and b are of type integer, which is a built-in data type. Bitwise Operator can be used directly on built-in data types.
class C
{
};
int main()
{
C c1(1), c2(0);
c3=c1 & c2;
return 0;
}
c1 and c2 are variables of type “class C”. Here compiler will generate an error as we are trying to use Bitwise Operator on user-defined data types.
The above example can be done by implementing methods or functions inside the class, but we choose operator overloading instead. The reason for this is, operator overloading gives the functionality to use the operator directly which makes code easy to understand, and even code size decreases because of it. Also, operator overloading does not affect the normal working of the operator but provides extra functionality to it.
Bitwise Operator Overloading
Now, if the user wants to use the Bitwise operator between objects of the class, then the user has to redefine the meaning of the Bitwise operator. Redefining the meaning of operators really does not change their original meaning, instead, they have been given additional meaning along with their existing ones.
Example 1:
C++
#include <iostream>
using namespace std;
class Bitwise {
private :
int value;
public :
Bitwise() {}
Bitwise( int v) { value = v; }
Bitwise operator&( const Bitwise& b)
{
Bitwise obj;
obj.value = value & b.value;
return obj;
}
void print()
{
cout << "value of (b1 & b2) is: " << value << endl;
}
};
int main()
{
Bitwise b1(1), b2(0);
Bitwise b3;
b3 = b1 & b2;
b3.print();
return 0;
}
|
Output
value of (b1 & b2) is: 0
Example 2:
C++
#include <iostream>
using namespace std;
class Bitwise {
private :
int value;
public :
Bitwise() {}
Bitwise( int v) { value = v; }
Bitwise operator|( const Bitwise& b)
{
Bitwise obj;
obj.value = value | b.value;
return obj;
}
void print()
{
cout << "value of (b1 | b2) is: " << value << endl;
}
};
int main()
{
Bitwise b1(1), b2(0);
Bitwise b3;
b3 = b1 | b2;
b3.print();
return 0;
}
|
Output
value of (b1 | b2) is: 1
Example 3:
C++
#include <iostream>
using namespace std;
class Bitwise {
private :
int value;
public :
Bitwise() {}
Bitwise( int v) { value = v; }
Bitwise operator^( const Bitwise& b)
{
Bitwise obj;
obj.value = value ^ b.value;
return obj;
}
void print()
{
cout << "value of (b1 ^ b2) is: " << value << endl;
}
};
int main()
{
Bitwise b1(2), b2(3);
Bitwise b3;
b3 = b1 ^ b2;
b3.print();
return 0;
}
|
Output
value of (b1 ^ b2) is: 1
Example 4:
C++
#include <iostream>
using namespace std;
class Bitwise {
private :
int value;
public :
Bitwise() {}
Bitwise( int v) { value = v; }
Bitwise operator<<( const Bitwise& b)
{
Bitwise obj;
obj.value = value << b.value;
return obj;
}
void print()
{
cout << "value of (b1 << b2) is: " << value << endl;
}
};
int main()
{
Bitwise b1(1), b2(3);
Bitwise b3;
b3 = b1 << b2;
b3.print();
return 0;
}
|
Output
value of (b1 << b2) is: 8
Example 5:
C++
#include <iostream>
using namespace std;
class Bitwise {
private :
int value;
public :
Bitwise() {}
Bitwise( int v) { value = v; }
Bitwise operator>>( const Bitwise& b)
{
Bitwise obj;
obj.value = value >> b.value;
return obj;
}
void print()
{
cout << "value of (b1 >> b2) is: " << value << endl;
}
};
int main()
{
Bitwise b1(16), b2(2);
Bitwise b3;
b3 = b1 >> b2;
b3.print();
return 0;
}
|
Output
value of (b1 >> b2) is: 4
Example 6:
C++
#include <bits/stdc++.h>
using namespace std;
class Integer {
private :
int i;
public :
Integer( int i = 0) { this ->i = i; }
Integer operator~()
{
Integer temp;
temp.i = ~i;
return temp;
}
void display() { cout << "i = " << i << endl; }
};
int main()
{
Integer i1(1);
cout << "Before applying ~ operator: " ;
i1.display();
Integer i2 = ~i1;
cout << "After applying ~ operator: " ;
i2.display();
}
|
Output
Before applying ~ operator: i = 1
After applying ~ operator: i = -2
Similar Reads
Operator Overloading in C++
in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning. In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Logical (&&, ||, !) Operator Overloading
Prerequisites: OperatorsOperator Overloading Logical operators are used for combining two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false. In C++, there are 3 logical operators: Logi
3 min read
Operator Overloading in Ruby
Ruby permits operator overloading, allowing one to define how an operator shall be used in a particular program. For example a '+' operator can be define in such a way to perform subtraction instead addition and vice versa. The operators that can be overloaded are +, -, /, *, **, %, etc and some ope
5 min read
Types of Operator Overloading in C++
C++ provides a special function to change the current functionality of some operators within its class which is often called as operator overloading. Operator Overloading is the method by which we can change some specific operators' functions to do different tasks. Syntax: Return_Type classname :: o
4 min read
Rules for operator overloading
In C++, following are the general rules for the things that are not allowed with operator overloading. 1) Only built-in operators can be overloaded. New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed. 4) O
3 min read
Bitwise Operators in C++
In C+, Bitwise Operators are the operators that are used to perform bit-level operations on the integers. While performing these operations, integers are considered as sequences of binary digits. These operators are useful for low-level programming, system programming, and optimizing performance. C+
6 min read
Bitwise Operators in C
In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if bot
7 min read
Different Ways of Operator Overloading in C++
In C++, operator overloading is the concept that allows us to redefine the behavior of the already existing operator for our class. C++ provides a special function called operator function that can be used to achieve operator overloading. In this article, we will learn the different ways in which we
6 min read
How to Overload == Operator in C++?
A class in C++ is the building block that leads to Object-Oriented programming. Class is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. The overloading of operators is a polymorphism that occurs a
2 min read
Bitwise Complement Operator (~ tilde)
Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in Java The bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1's become 0's and vice versa. The operator for t
3 min read