Skip to content
Related Articles
Open in App
Not now

Related Articles

C++ Bitwise Operator Overloading

Improve Article
Save Article
  • Last Updated : 27 Nov, 2022
Improve Article
Save Article

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++




// C++ program to overload Bitwise AND (&) operator
#include <iostream>
using namespace std;
  
class Bitwise {
private:
    int value;
  
public:
    // Non-parameterized constructor
    Bitwise() {}
  
    // parameterized constructor
    Bitwise(int v) { value = v; }
  
    // This is automatically called
    // when '&' operator is
    // used between b1 and b2.
    Bitwise operator&(const Bitwise& b)
    {
        Bitwise obj;
        obj.value = value & b.value;
        return obj;
    }
  
    // method to display value
    void print()
    {
        cout << "value of (b1 & b2) is: " << value << endl;
    }
};
  
int main()
{
    // Assigning by overloading constructor
    Bitwise b1(1), b2(0);
    Bitwise b3;
  
    // overloading & operator and storing the result in b3
    b3 = b1 & b2; // b3=b1.&(b2);
    b3.print();
    return 0;
}

Output

value of (b1 & b2) is: 0

Example 2:

C++




// C++ program to overload Bitwise OR (|) operator
#include <iostream>
using namespace std;
  
class Bitwise {
private:
    int value;
  
public:
    // Non-parameterized constructor
    Bitwise() {}
  
    // parameterized constructor
    Bitwise(int v) { value = v; }
  
    // This is automatically called
    // when '|' operator is
    // used between b1 and b2.
    Bitwise operator|(const Bitwise& b)
    {
        Bitwise obj;
        obj.value = value | b.value;
        return obj;
    }
  
    // method to display value
    void print()
    {
        cout << "value of (b1 | b2) is: " << value << endl;
    }
};
  
int main()
{
    // Assigning by overloading constructor
    Bitwise b1(1), b2(0);
    Bitwise b3;
  
    // overloading | operator and storing the result in b3
    b3 = b1 | b2; // b3=b1.|(b2);
    b3.print();
    return 0;
}

Output

value of (b1 | b2) is: 1

Example 3:

C++




// C++ program to overload Bitwise XOR (^) operator
#include <iostream>
using namespace std;
  
class Bitwise {
private:
    int value;
  
public:
    // Non-parameterized constructor
    Bitwise() {}
  
    // parameterized constructor
    Bitwise(int v) { value = v; }
  
    // This is automatically called
    // when '^' operator is
    // used between b1 and b2.
    Bitwise operator^(const Bitwise& b)
    {
        Bitwise obj;
        obj.value = value ^ b.value;
        return obj;
    }
  
    // method to display value
    void print()
    {
        cout << "value of (b1 ^ b2) is: " << value << endl;
    }
};
  
int main()
{
    // Assigning by overloading constructor
    Bitwise b1(2), b2(3);
    Bitwise b3;
  
    // overloading ^ operator and storing the result in b3
    b3 = b1 ^ b2; // b3=b1.^(b2);
    b3.print(); // 2^3=1
    return 0;
}

Output

value of (b1 ^ b2) is: 1

Example 4: 

C++




// C++ program to overload Bitwise left shift (<<) operator
#include <iostream>
using namespace std;
  
class Bitwise {
private:
    int value;
  
public:
    // Non-parameterized constructor
    Bitwise() {}
  
    // parameterized constructor
    Bitwise(int v) { value = v; }
  
    // This is automatically called
    // when '<<' operator is
    // used between b1 and b2.
    Bitwise operator<<(const Bitwise& b)
    {
        Bitwise obj;
        obj.value = value << b.value;
        return obj;
    }
  
    // method to display value
    void print()
    {
        cout << "value of (b1 << b2) is: " << value << endl;
    }
};
  
int main()
{
    // Assigning by overloading constructor
    Bitwise b1(1), b2(3);
    Bitwise b3;
  
    // overloading << operator and storing the result in b3
    b3 = b1 << b2; // b3=b1.<<(b2);
    b3.print(); // 1<<3=8
    return 0;
}

Output

value of (b1 << b2) is: 8

Example 5: 

C++




// C++ program to overload Bitwise Right shift (>>) operator
#include <iostream>
using namespace std;
  
class Bitwise {
private:
    int value;
  
public:
    // Non-parameterized constructor
    Bitwise() {}
  
    // parameterized constructor
    Bitwise(int v) { value = v; }
  
    // This is automatically called
    // when '>>' operator is
    // used between b1 and b2.
    Bitwise operator>>(const Bitwise& b)
    {
        Bitwise obj;
        obj.value = value >> b.value;
        return obj;
    }
  
    // method to display value
    void print()
    {
        cout << "value of (b1 >> b2) is: " << value << endl;
    }
};
  
int main()
{
    // Assigning by overloading constructor
    Bitwise b1(16), b2(2);
    Bitwise b3;
  
    // overloading >> operator and storing the result in b3
    b3 = b1 >> b2; // b3=b1.>>(b2);
    b3.print(); // 1<<3=8
    return 0;
}

Output

value of (b1 >> b2) is: 4

Example 6: 

C++




// C++ program to overload Bitwise NOT (~) operator
  
#include <bits/stdc++.h>
using namespace std;
  
class Integer {
private:
    int i;
  
public:
    // Parameterised constructor
    Integer(int i = 0) { this->i = i; }
  
    // Overloading the ~ operator
    Integer operator~()
    {
        Integer temp;
        temp.i = ~i;
        return temp;
    }
  
    // Function to display the value of i
    void display() { cout << "i = " << i << endl; }
};
  
// Driver Code
int main()
{
    Integer i1(1);
  
    cout << "Before applying ~ operator: ";
    i1.display();
  
    // Using the Bitwise (~) operator
    Integer i2 = ~i1; // i1.operator~()
  
    cout << "After applying ~ operator: ";
    i2.display();
}

Output

Before applying ~ operator: i = 1
After applying ~ operator: i = -2

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!