Bitwise Operators
At the bit level, operations are carried out using a bitwise operator. They translate our input values into binary format, process them using the appropriate operator, and output the results.
Operator | Description |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise Complement |
>> | Shift Right Operator |
<< | Shift Left Operator |
#include <iostream>
using namespace std;
int main()
{
int a = 13; //1101
int b = 5; //101
cout << "The value of a & b is " << (a & b) << endl;
cout << "The value of a | b is " << (a | b) << endl;
cout << "The value of a ^ b is " << (a ^ b) << endl;
cout << "The value of ~a is " << (~a) << endl;
cout << "The value of a >> 2 is " << (a >> 2) << endl;
cout << "The value of a << 2 is " << (a << 2) << endl;
}
Output:
The value of a & b is 5
The value of a | b is 13
The value of a ^ b is 8
The value of ~a is -14
The value of a >> 2 is 3
The value of a << 2 is 52
Assignment Operators
Values are assigned using assignment operators. They will be incorporated into nearly every programme we create.
int a = 0;
int b = 1;
Operator | Description |
= | It assigns the right side operand value to the left side operand. |
+= | It adds the right operand to the left operand and assigns the result to the left operand. |
-= | It subtracts the right operand from the left operand and assigns the result to the left operand. |
*= | It multiplies the right operand with the left operand and assigns the result to the left operand. |
/= | It divides the left operand with the right operand and assigns the result to the left operand. |
Inherent Operator Priority and Associativity
Operator priority
It assists us in solving an equation by allowing us to distinguish which operator comes before another. Think about the phrase a+b*c. Given that the precedence of the multiplication operator is higher than the precedence of the addition operator, multiplication between a and b will now be completed before the addition operation.
Associativity of operators
When two or more operators with the same precedence are combined in an expression, it aids in solving the expression. It aids in determining whether to begin solving the expression comprising operators with the same precedence from the left to the right or vice versa.