Comparison, logical, and bitwise operators
Build Boolean conditions, understand short-circuit evaluation, and use bitwise operators carefully.
Learning objectives
- Use relational operators to compare values.
- Combine conditions with logical operators.
- Explain short-circuit evaluation.
- Distinguish logical and bitwise operations.
Prerequisites: variables, numeric types, and simple expressions. Review Variables and Expressions and arithmetic first.
Relational operators
Relational operators compare values and produce a Boolean result.
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
Logical operators
&& means logical AND, || means logical OR, and ! means logical NOT. They combine or reverse Boolean conditions.
int age{20};
bool has_id{true};
bool can_enter = age >= 18 && has_id;
// true && true → trueShort-circuit evaluation
For A && B, if A is false, B is not evaluated because the result is already false. For A || B, if A is true, B is not evaluated because the result is already true. This is called short-circuit evaluation.
int numerator{10};
int denominator{0};
if (denominator != 0 && numerator / denominator > 2) {
// division is evaluated only when denominator is non-zero
}The left condition checks the divisor first. If it is false, the right side is skipped, so the program does not evaluate a division by zero. The order of conditions can therefore be important.
Logical versus bitwise
Logical operators work with truth values. Bitwise operators work on individual bits of integral values: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), and >> (right shift). Do not confuse && with &, or || with |.
Bitwise operations are useful for flags and low-level data formats, but shift behavior and signed representations require care. Learn the exact rules before using shifts in production code.
Common mistakes
- Writing
=instead of==in a comparison. - Using
&when you intended logical AND. - Assuming both sides of a logical expression always execute.
- Writing a condition that indexes an array before checking its bounds.
Practice
- Write a condition for an age between 18 and 60 inclusive.
- Use short-circuit logic to check a divisor before dividing.
- Predict the truth value of
true || false && falseusing precedence. - For two small non-negative integers, calculate bitwise AND, OR, and XOR in binary.
Practice solutions
Show reasoned solutions
- Use
age >= 18 && age <= 60. Both endpoints are included. - Use
index < values.size() && values[index] == target. The left operand is evaluated first; if it is false,&&skips the right operand, so the invalid index is never accessed. &&has higher precedence than||, so the expression groups astrue || (false && false)and evaluates to true.- For 5 (
0101) and 3 (0011): AND is0001(1), OR is0111(7), and XOR is0110(6).
Check your understanding
Why should the non-zero check appear before the division?
Answer: Because && evaluates left to right and skips the right operand when the left operand is false. When the divisor is zero, the division expression is not evaluated.
Summary
Use comparisons to create Boolean values, logical operators to combine conditions, and bitwise operators only when you intend to work with bits. Short-circuit behavior can make a condition safer.