When you first learn math, you’re often taught a rule called PEMDAS, which helps you remember the order of operations: Parentheses, Exponents, Multiplication, Division, Addition, and Subtraction with a left to right weight. This is the rule that tells you how to solve math problems like this:
3 + 5 * 2
According to PEMDAS, you do the multiplication first:
3 + (5 * 2) = 3 + 10 = 13
You can use parentheses to force certain parts to be calculated first. For example:
(3 + 5) * 2 = 8 * 2 = 16
Dennis Ritchie’s View on Operator Precedence
When Dennis Ritchie, the creator of the C programming language, designed C, he followed a similar idea of operator precedence to PEMDAS. However, he created rules not only for basic math operations like + and * but also for more complex things like bitwise operations, comparisons, and logical operations.
Ritchie believed that bitwise operators (which deal with manipulating individual bits in numbers) should have had higher precedence than comparison operators (like == and !=). But in C—and in languages that took inspiration from C, like PHP and JavaScript—the precedence order places bitwise operators below comparison operators.
Operator Precedence in C, PHP, and JavaScript In C, PHP, and JavaScript, operators have different levels of precedence. Let’s break down how this works:
Basic Arithmetic
a + b * c
This follows PEMDAS, where multiplication happens before addition.
Logical Operators
a || b && c
Here, the && (logical AND)
has higher precedence than || (logical OR)
. This means b && c
happens first, then a ||
is evaluated.
Bitwise vs Comparison
Here’s where Dennis Ritchie’s concern comes in: a & b == c
The comparison b == c happens first, and then the result is bitwise ANDed (&) with a. Ritchie thought it would make more sense for the bitwise operator to have higher precedence, like this:
(a & b) == c
However, that’s not how it works in these languages by default.
Bitwise Operators in C, PHP, and JavaScript
Bitwise operators work directly on the binary (bit) representation of numbers. Here’s a simple explanation of how these operators work:
Bitwise AND (&)
5 & 3
results in 1. This is because the binary representation of 5 is 101 and the binary of 3 is 011. The bitwise AND operation only keeps the 1 bits that are in both numbers, resulting in 001, which is 1 in decimal.
Bitwise OR ( | ) |
5 | 3
results in 7. The binary of 5 is 101 and the binary of 3 is 011. The bitwise OR keeps all the 1 bits from both numbers, resulting in 111, which is 7 in decimal.
Bitwise XOR (^)
5 ^ 3
results in 6. XOR returns 1 only when one of the bits is 1 and the other is 0. For 101 and 011, this results in 110, which is 6 in decimal.
Bitwise Shift Left («)
5 << 1
results in 10. Shifting the bits of 5 (which is 101 in binary) one position to the left gives 1010, which is 10 in decimal.
Bitwise Shift Right (»)
5 >> 1
results in 2. Shifting the bits of 5 one position to the right gives 010, which is 2 in decimal.
The Difference in Python and Ruby
Bitwise operators have higher precedence than comparison operators. So in these languages, the expression:
a & b == c
Is interpreted as:
(a & b) == c
This is the opposite of what happens in C, PHP, and JavaScript, where b == c is evaluated first.
Mathematical operations in C, PHP, and JavaScript follow the idea of PEMDAS, where multiplication and division have higher precedence than addition and subtraction. Bitwise operators have lower precedence than comparison operators in C, PHP, and JavaScript, which is why a & b == c
is interpreted as a & (b == c)
.
Dennis Ritchie’s suggestion about bitwise operators having higher precedence didn’t make it into C, but understanding these rules can help you avoid mistakes when writing code, especially when working with bitwise operations or mixing comparisons and logic.
Full Operator Precedence in C and PHP
Function call, member access: (), [], ->, .
Unary operators: ++, --, +, -, !, ~, &, *, sizeof, type casts
Multiplication/Division/Modulo: *, /, %
Addition/Subtraction: +, -
Bitwise Shift: <<, >>
Relational Operators: <, <=, >, >=
Equality Operators: ==, !=
Bitwise AND: &
Bitwise XOR: ^
Bitwise OR: |
Logical AND: &&
Logical OR: ||
Ternary Operator: ? :
Assignment Operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
Comma Operator: ,