Bitwise OR

In short: The bitwise OR operator (|) compares two numbers bit by bit, setting each result bit to 1 if either input bit is 1. It's commonly used to set specific bits or combine flags into a single value.

The OR operation takes two bits and returns 1 if either of the bits are 1. Otherwise, it returns 0.

1 | 1 → 1 1 | 0 → 1 0 | 1 → 1 0 | 0 → 0

Think of it like a bucket with two holes in it. If both holes are closed, no water comes out. If either hole is open, or if both are open, water comes out.

When performing OR on two integers, the OR operation is calculated on each pair of bits (the two bits at the same index in each number).

5 | 6 // gives 7 // At the bit level: // 0101 (5) // | 0110 (6) // = 0111 (7)

Frequently Asked Questions

What does the bitwise OR operator do?

It compares two numbers bit by bit, producing a 1 in each position where at least one of the inputs has a 1.

What is bitwise OR used for?

Setting particular bits to 1 and combining boolean flags packed into a single integer, for example READ | WRITE.

What's the difference between | and ||?

| is the bitwise OR on the individual bits of integers; || is the logical OR on boolean values, and it short-circuits.

Last updated: June 17, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .