Back to C Programming
2026-07-156 min read

Bitwise OR (|) Operator (C Programming)

Learn Bitwise OR (|) Operator (C Programming) step by step with clear examples and exercises.

Why This Matters

The bitwise OR operator (|) is a fundamental concept in C programming that provides an essential tool for advanced topics such as low-level system programming, data manipulation, and solving complex problems that test your understanding of bit manipulation and the inner workings of C.

Prerequisites

Before delving into the bitwise OR operator, it is crucial to have a firm grasp of:

  1. Basic C syntax and data types (int, char, float, etc.)
  2. Variables and their declaration
  3. Arithmetic operators (+, -, *, /)
  4. Assignment operator (=)
  5. Relational operators (<, >, ==, !=, <=, >=)
  6. Control structures (if, if...else, switch)
  7. Functions and function prototypes
  8. Understanding of bitwise AND (&) and bitwise XOR (^) operators
  9. Basic C memory management concepts (pointers, arrays, etc.)
  10. Comprehension of logical operators (&&, ||)
  11. Knowledge of conditional and ternary operators (?:)
  12. Familiarity with loops (for, while, do...while)

Core Concept

The bitwise OR operator (|) performs a bit-by-bit comparison between two binary numbers. It sets the result's corresponding bit to 1 if at least one of the operands' bits is 1; otherwise, it sets the result's bit to 0.

Here's an example using decimal numbers:

int a = 6; // binary: 110
int b = 3; // binary: 011
int c = a | b; // binary: 111 (1 OR 1 = 1, 1 OR 0 = 1)

In this example, the bitwise OR operation on a and b results in c, which is equal to 7 in decimal. Note that the bitwise OR operator does not care about the order of the operands, so b | a would also produce the same result (111 or 7).

Bitwise OR with Multiple Operands

You can apply the bitwise OR operator to multiple operands as well. The operation will be performed on each pair of bits from left to right, and the resulting bits will be set according to the rules mentioned earlier:

int a = 6; // binary: 110
int b = 3; // binary: 011
int c = 5; // binary: 101
int d = a | b | c; // binary: 111 (1 OR 1 = 1, 1 OR 0 = 1)

In this example, d is equal to 7 in decimal.

Bitwise OR and Logical OR (||)

It's essential to understand the difference between bitwise OR and logical OR (||) as they operate differently:

  • The bitwise OR operator (|) compares individual bits of its operands and sets the result's corresponding bit based on those comparisons.
  • The logical OR operator (||) evaluates its operands in a left-to-right manner, returning true if either operand is true; otherwise, it returns false.

Worked Example

Let's write a simple C program that demonstrates the use of the bitwise OR operator:

#include <stdio.h>

int main() {
int a = 6;
int b = 3;
int c = a | b;

printf("a: %d\n", a);
printf("b: %d\n", b);
printf("a | b: %d\n", c);

return 0;
}

When you run this program, it will output:

a: 6
b: 3
a | b: 7

Common Mistakes

  1. Forgetting to include the header file: Make sure you have #include at the beginning of your C files whenever you use printf.
  2. Incorrect bitwise OR usage: Be careful when using the bitwise OR operator, as it operates on individual bits and not the entire numbers. For example, if you want to check if a number is odd or even, you should use the bitwise AND operator (&) with 1 instead of the bitwise OR operator (|).
  3. Not understanding the result: Remember that the bitwise OR operator sets the result's corresponding bit to 1 if at least one of the operands' bits is 1. This can lead to unexpected results when dealing with complex expressions or large numbers.
  4. Misusing the bitwise OR operator for logical OR (||): The bitwise OR operator should not be used interchangeably with the logical OR operator (||). While both operators perform similar functions, they operate differently and can lead to incorrect results when used improperly.
  5. Neglecting to handle negative numbers: When working with bitwise operators, it's important to remember that C represents signed integers using two's complement notation. This means that the sign bit (the leftmost bit) determines whether a number is positive or negative. Be aware of this when performing bitwise operations on signed integers.
  6. Not considering edge cases: Always consider edge cases, such as all-zero and all-one inputs, to ensure your code behaves correctly in various scenarios.

Common Mistakes (subheadings)

  1. Forgetting to include header files
  2. Incorrect bitwise OR usage
  3. Not understanding the result
  4. Misusing bitwise OR for logical OR (||)
  5. Neglecting to handle negative numbers
  6. Not considering edge cases

Practice Questions

  1. Write a C program that checks whether a given number is even or odd using the bitwise AND operator (&) and the bitwise OR operator (|).
  2. Given two 32-bit integers a and b, write a C function that calculates their bitwise XOR (exclusive OR) using the bitwise XOR operator (^).
  3. Write a C program that takes two binary numbers as input, performs a bitwise AND operation on them, and outputs the result.
  4. Write a C function that counts the number of set bits in a given 32-bit integer using bitwise operations.
  5. Write a C program that finds the maximum number between two integers using only bitwise operators.
  6. Write a C program that swaps two variables using only bitwise operators.
  7. Write a C function that checks if a given number is a power of 2 using only bitwise operators.
  8. Write a C program that finds the first set bit (from right to left) in a given positive integer using only bitwise operators.
  9. Write a C program that calculates the sum of two numbers without using any arithmetic operators (+, -, *, /).
  10. Write a C function that compares two strings lexicographically using only bitwise operators and logical operators (&&, ||).

FAQ

  1. What is the difference between the bitwise AND, OR, and XOR operators in C?
  • The bitwise AND operator (&) sets the result's corresponding bit to 1 if both operands' bits are 1; otherwise, it sets the result's bit to 0.
  • The bitwise OR operator (|) sets the result's corresponding bit to 1 if at least one of the operands' bits is 1; otherwise, it sets the result's bit to 0.
  • The bitwise XOR operator (^) sets the result's corresponding bit to 1 if exactly one of the operands' bits is 1; otherwise, it sets the result's bit to 0.
  1. Why are bitwise operators important in C programming?

Bitwise operators allow you to perform complex operations on individual bits of a number, which can be crucial for low-level system programming and data manipulation. They enable you to create efficient algorithms, optimize memory usage, and interact with hardware more effectively. Additionally, understanding bitwise operators can help you solve challenging interview questions related to bit manipulation and C programming.

  1. How do I check if a number is odd or even using the bitwise AND operator (&) and the bitwise OR operator (|)?

To check if a number is odd, you can use the following expression: (number & 1) == 1. To check if a number is even, you can use (number & 1) == 0. This works because the rightmost bit of any positive integer (when represented in binary) is always either 0 or 1.

  1. What are some common uses of bitwise operators in C programming?

Bitwise operators have numerous applications in C programming, including:

  • Low-level system programming and device drivers
  • Data compression algorithms
  • Encryption and decryption routines
  • Efficient implementation of sorting and searching algorithms
  • Implementation of custom data structures like bitmaps or Bloom filters
  • Optimizing memory usage by packing multiple values into a single word
  • Solving interview problems related to bit manipulation and C programming