Understanding C++ Bitwise Operators: A Deep Dive into Operator Precedence
Bitwise operators in C++ are fundamental to low-level programming, enabling manipulation of data at the binary level. However, understanding the priority of these operators is crucial for writing efficient and error-free code. Let's delve into the world of bitwise operators and explore their precedence rules.
Bitwise Operators: A Quick Refresher
Before we dive into precedence, let's briefly recap the six bitwise operators in C++:
- Bitwise AND (&): Performs AND operation on every bit of two numbers.
- Bitwise OR (|): Performs OR operation on every bit of two numbers.
- Bitwise XOR (^): Performs XOR operation on every bit of two numbers.
- Bitwise NOT (~): Performs NOT operation on every bit of a number.
- Bitwise Left Shift (<<): Shifts the bits of the number to the left by the specified number of places.
- Bitwise Right Shift (>>): Shifts the bits of the number to the right by the specified number of places.
Bitwise Operators Precedence: A Closer Look
The precedence of operators determines the order in which operations are performed. In C++, bitwise operators follow a specific order of precedence, which is as follows:

| Operator | Precedence |
|---|---|
| ~ | Highest |
| <<, >> | High |
| &, ^, | | Low |
As you can see, the bitwise NOT operator (~), also known as the unary operator, has the highest precedence. This means that it will be evaluated first in an expression. The left shift (<<) and right shift (>>) operators have the next highest precedence, followed by the bitwise AND (&), XOR (^), and OR (|) operators, which share the same precedence.
Examples: Bitwise Operators in Action
Let's consider a few examples to illustrate the precedence of bitwise operators in C++.
Consider the following expression:

a = 60; // 60 = 0011 1100 in binary
b = 13; // 13 = 0000 1101 in binary
c = 0;

c = a & b;
In this example, the bitwise AND operator (&) has a lower precedence than the assignment operator (=). Therefore, the bitwise operation is performed first, and then the result is assigned to 'c'.
Now, let's consider the following expression:
a = 60; // 60 = 0011 1100 in binary
b = 13; // 13 = 0000 1101 in binary
c = 0;
c = a << 2 & b;
In this case, the left shift operator (<<) has a higher precedence than the bitwise AND operator (&). Therefore, the left shift operation is performed first, and then the result is ANDed with 'b'.
Bitwise Operator Precedence vs. Associativity
It's essential to understand that while precedence determines the order of operations, associativity determines how operations are grouped when operators have the same precedence. Bitwise operators in C++ are left-associative, meaning that operations are grouped from left to right when operators have the same precedence.
For example, consider the following expression:
a = 60; // 60 = 0011 1100 in binary
b = 13; // 13 = 0000 1101 in binary
c = 0;
c = a & b | a ^ b;
In this case, the bitwise AND (&) and OR (|) operators have the same precedence, and the bitwise XOR (^) operator also has the same precedence. Since these operators are left-associative, the expression is evaluated as follows:
c = (a & b) | (a ^ b);
First, the bitwise AND operation is performed, and then the result is ORed with the result of the bitwise XOR operation.
Conclusion: Mastering Bitwise Operator Precedence
Understanding the precedence of bitwise operators in C++ is crucial for writing efficient and error-free code. By familiarizing yourself with the order of operations and the associativity of these operators, you can effectively manipulate data at the binary level and unlock the full potential of C++. Keep practicing and experimenting with bitwise operators to solidify your understanding and become a proficient C++ programmer.






















