Bitwise OR merges two bit patterns: a result bit is 1 if either operand has it. This question from our C Programming Quiz App uses two numbers whose bits don’t overlap — which makes OR behave exactly like addition, and that’s precisely what makes it a good test of whether you’re thinking in bits.
The Quiz Question
int a = 4, b = 2;
printf("%d", a | b);
What is printed by this code?
- 4
- 6
- 2
- 0
The Correct Answer: 6
Write the operands in binary and OR each column:
4 = 1 0 0
2 = 0 1 0
─────────── |
1 1 0 = 6
Each number contributes its own bit; the union is 110 = 6. Verified on gcc 13.3 and Apple clang 21, warning-free:
$ gcc -Wall -Wextra or.c && ./a.out 6
Because 4 and 2 share no bits, 4 | 2 equals 4 + 2 — but that’s a coincidence of non-overlapping bits, not a rule. The proof is what happens when bits do overlap (from the same verified run):
printf("%d\n", 4 | 4); /* 4 — not 8! a set bit stays set, no carrying */
Why Each Wrong Answer Is Wrong
Why not 4?
4 would mean b contributed nothing — true only if its bits were already present in a (like 4 | 4 or 6 | 2). But 2’s bit is new, so the result grows.
Why not 2?
Same reasoning mirrored — OR never discards bits from either side. The result always has at least every bit of both operands: a | b >= a and >= b, always.
Why not 0?
0 is what bitwise AND gives for these operands: 4 & 2 = 0, since they share no bits. OR and AND are duals — OR unions the bits, AND intersects them. Picking 0 means applying the intersection where the union was asked.
The Flags Idiom — Where | Lives in Real Code
Bitwise OR is how C combines options into one integer. Every flag gets its own bit (built with left shift), and OR packs them:
fd = open(path, O_RDWR | O_CREAT | O_APPEND); /* three options, one int */
flags |= FLAG_VISIBLE; /* turn a bit on */
The 4 | 4 = 4 behavior is a feature here: OR-ing a flag that’s already set changes nothing — setting is idempotent, which is exactly what you want from “turn this option on”. The other members of the family: & tests bits, &~ clears them, ^ toggles them.
Don’t confuse | with ||: the double-pipe is logical OR, which collapses everything to a 1-or-0 verdict — 4 || 2 is 1, not 6 (verified in the same run; see the logical OR post).
Frequently Asked Questions
Is bitwise OR the same as addition?
Only when the operands share no bits: 4 | 2 = 6 = 4 + 2, but 4 | 4 = 4 while 4 + 4 = 8. OR has no carrying — a set bit simply stays set.
What is the difference between | and || in C?
| is bitwise: it merges bit patterns (4 | 2 = 6) and always evaluates both operands. || is logical: it yields 0 or 1 (4 || 2 = 1) and short-circuits.
How do I set, clear, and test a flag bit in C?
Set with OR: flags |= F. Clear with AND-NOT: flags &= ~F. Test with AND: if (flags & F). Toggle with XOR: flags ^= F.
Related Reading
- Bitwise AND in C – Why 2 & 3 Is 2
- Bitwise XOR in C – Why 5 ^ 3 Is 6
- Left Shift Operator in C – Why 1 << 3 Is 8
- C Aptitude Questions and Answers
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
This question is #153 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →