Operator Precedence in Bitwise Operators
Bitwise operators operate on binary representations of integers. The precedence of bitwise operators from highest to lowest is as follows:
- Bitwise NOT ~
- Bitwise SHIFT <<, >>
- Bitwise AND &
- Bitwise XOR ^
- Bitwise OR |
Below is the implementation of Operator Precedence in Bitwise Operators:
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 9, c = 3;
int result;
// Bitwise AND has higher precedence than Bitwise OR
result = a & b | c;
cout << "a & b | c = " << result << endl;
// Bitwise AND has higher precedence than Bitwise XOR
result = a ^ b & c;
cout << "a ^ b & c = " << result << endl;
// Bitwise Shift has higher precedence than Bitwise OR
result = a | b << 2;
cout << "a | b << 2 = " << result << endl;
return 0;
}
Output
a & b: 12 a | b: 61 a ^ b: 49 ~a: -61 a << 2: 240 a >> 2: 15
Operator Precedence in Programming
Operator Precedence, also known as operator hierarchy, is a set of rules that controls the order in which operations are performed in an expression without parentheses. It is a fundamental concept in programming languages and is crucial for writing correct and efficient code.
Table of Content
- What is Operator Precedence?
- Operator Precedence in Arithmetic Operators
- Operator Precedence in Relational Operators
- Operator Precedence in Logical Operators
- Operator Precedence in Assignment Operators
- Operator Precedence in Bitwise Operators
- Operator Precedence in Conditional (Ternary) Operator
- Operator Precedence in Unary Operators
- Operator Precedence in Member Access Operators
- Operator Precedence in Type Cast Operators
- Importance of Operator Precedence