- How the
|operator computes the bitwise OR of two integers - How binary representation matters for bitwise operations
- The bit-by-bit truth table:
0 | 0 = 0, everything else is1 - How to use OR to set specific bits (the opposite of masking)
- How to combine multiple flags into one value
The bitwise OR operator
The bitwise OR operator | compares two integers bit by bit. For each bit position, if either corresponding bit is 1, the result bit is 1. The result bit is 0 only when both input bits are 0. This is the complement of AND's behavior.
Don't confuse | (bitwise OR) with || (logical OR). | operates on each bit of two integers and produces an integer. || operates on two boolean values (treating nonzero as true) and produces 0 or 1. They look similar but are completely different operators.
Common uses of |:
- Setting bits — turn specific bits on:
n | (1 << k)sets bitk. - Combining flags — merge several flags into one integer:
READ | WRITE | EXEC. - Merging bit masks — combine overlapping bit patterns.
- Building packed data — encode multiple small values into one integer (e.g., RGB colors).
a | b sets each bit of the result to 1 wherever either a or b has a 1. It's the "keep every bit that's on in either operand" operator.
Truth table
For each pair of bits, the OR result is:
| Bit a | Bit b | a | b |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
In words: the result is 0 only when both input bits are 0. Otherwise it's 1. Applying this rule at every bit position gives the bitwise OR.
For example, 12 | 10:
Reading bit by bit from left to right:
- Bit 3 (8's place): 1 | 1 = 1
- Bit 2 (4's place): 1 | 0 = 1
- Bit 1 (2's place): 0 | 1 = 1
- Bit 0 (1's place): 0 | 0 = 0
Result: 1110 in binary = 14 in decimal. So 12 | 10 = 14.
Complete C Program
Here is the program — bitwise OR on two integers:
Program: Bitwise OR of two integers
#include <stdio.h>
int main(void) {
int a, b, result;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
result = a | b;
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("a | b = %d\n", result);
return 0;
}
A variant that also shows the binary representation:
Variant: Show binary representation
#include <stdio.h>
void print_binary(int n) {
for (int i = 7; i >= 0; i--) {
printf("%d", (n >> i) & 1);
if (i % 4 == 0 && i != 0) printf(" ");
}
printf("\n");
}
int main(void) {
int a, b, result;
printf("Enter two integers (0-255): ");
scanf("%d %d", &a, &b);
result = a | b;
printf("\na = ");
print_binary(a);
printf("b = ");
print_binary(b);
printf("a | b = ");
print_binary(result);
printf("\nDecimal result: %d\n", result);
return 0;
}
The print_binary helper iterates from bit 7 down to bit 0, using (n >> i) & 1 to isolate each bit — the AND operator appears here twice (once to compute the OR result, once to extract bits for display). This is the standard way to display binary in C, since printf has no %b specifier.
Program Output
Sample run of the basic program:
Enter two integers: 12 10
a = 12
b = 10
a | b = 14
As computed above: 12 (1100) | 10 (1010) = 1110 (14). Correct.
More examples:
Enter two integers: 1 2
a = 1
b = 2
a | b = 3
1 = 01, 2 = 10, OR is 11 = 3. This is the classic "combine flags" pattern — two separate flags merge into one value that has both bits set.
And:
Enter two integers: 100 7
a = 100
b = 7
a | b = 103
100 = 1100100, 7 = 0000111, OR is 1100111 = 103. The low 3 bits of 100 are 100 (binary), and OR with 111 gives 111, which equals 7 in decimal. The upper bits of 100 are preserved.
The variant with binary display:
Enter two integers (0-255): 12 10
a = 0000 1100
b = 0000 1010
a | b = 0000 1110
Decimal result: 14
The binary view makes the operation obvious: wherever either 12 or 10 has a 1, the result has a 1. Only bit 0 (the 1's place) is 0 in both, so it's 0 in the result.
Step-by-Step Explanation
1. Declarations: int a, b, result; — bitwise OR works on integers (or any integer type: char, short, long). The result has the same type as the operands after the usual arithmetic promotions.
2. Input: scanf("%d %d", &a, &b) reads two integers. The & here is the address-of operator, not the bitwise AND operator — same symbol, different context.
3. The OR operation: result = a | b computes the bitwise OR. Each bit of result is set to 1 if the corresponding bit is 1 in either a or b. For 12 | 10: 1100 | 1010 = 1110 = 14.
4. Why is it useful? The OR operator is the primary tool for setting bits. For example:
n | 1— sets the lowest bit to 1, makingnodd (if it was even).n | (1 << k)— sets bitkto 1, leaving other bits unchanged.flags | WRITE— turns on the WRITE flag, regardless of its previous state.RED | GREEN— combines two color components into one integer.
5. Output format: %d prints the integer result in decimal. To see the binary, you'd need a custom function (as in the variant).
6. Precedence: The bitwise OR has lower precedence than most other operators, including equality, relational, and even bitwise AND and XOR. So a | b == c means a | (b == c), not (a | b) == c — a classic source of bugs. Always parenthesize: (a | b) == c.
a | b is bit-by-bit: 1 if either bit is 1. It's the "set bit" operator — use it to turn specific bits on, combine flags, or merge bit patterns. Watch out for | vs || and for precedence with ==.
Frequently Asked Questions
1. What's the difference between | and ||?
| is bitwise OR — it operates on each bit of two integers and produces an integer. || is logical OR — it treats its operands as booleans (0 = false, nonzero = true) and produces 0 or 1. For example, 5 | 3 = 7 (bitwise), but 5 || 3 = 1 (both nonzero, so result is true = 1). Use | for bit manipulation, || for boolean logic.
2. How do I set a specific bit using OR?
Use n | (1 << k). This sets bit k to 1, leaving all other bits unchanged. For example, to set bit 3 (the 8's place): n = n | (1 << 3) or equivalently n |= (1 << 3). To set multiple bits at once, OR the masks together: n | 0b1010 sets bits 1 and 3. This is the fundamental way to turn on flags in C.
3. How do I combine multiple flags into one value?
Use OR: flags = READ | WRITE | EXEC;. Each flag is a power of 2 (1, 2, 4, 8, …), so they occupy distinct bit positions. OR combines them without conflict — READ | WRITE gives a value with both bits set. To test whether a flag is set: if (flags & READ). To remove a flag: flags &= ~READ. This pattern is ubiquitous in systems programming (permissions, file modes, feature bits).
4. Why is a | b == c often a bug?
Because == has higher precedence than |. So a | b == c parses as a | (b == c), not (a | b) == c. Since b == c evaluates to 0 or 1, the result is usually a | 0 = a or a | 1. If you meant "the OR equals c", write (a | b) == c. Better yet, always parenthesize bitwise expressions.
5. Can I use | on negative numbers?
Yes — the operator works on the binary representation of negative numbers (which is two's complement on all modern systems). For example, −1 | 5 = −1, because −1 is all 1s in binary, and OR with anything gives all 1s. −2 | 3 = −1 (...11111110 | 00000011 = ...11111111 = −1). The results depend on the bit patterns, which for negatives are implementation-defined (but in practice two's complement, uniform across platforms).
💡 Tip: once this works, try these classic OR applications: (a) set flags with flags |= FLAG_X, (b) combine color components (red | (green << 8) | (blue << 16) for RGB), (c) turn on the lowest bit to force a number odd (n | 1), (d) merge two 4-bit values into one byte ((hi << 4) | lo). Each is a small extension of the same operator.