Explain the bitwise OR (|) operator in JavaScript.
Explain the bitwise OR (|) operator in JavaScript.
237
30-Oct-2023
Updated on 31-Oct-2023
Aryan Kumar
31-Oct-2023The bitwise OR (|) operator in JavaScript is used to perform a bitwise OR operation on the individual bits of two numbers. It returns a number where each bit is set (1) if at least one of the corresponding bits of the two numbers being ORed is 1.
Here's how the bitwise OR operator works with a simple example:
In this example, the OR operation is performed on num1 and num2. The result has a 1 at any position where at least one of the input numbers has a 1. So, 5 | 3 results in 7 because the bits in the binary representation differ at the 2^0, 2^1, and 2^2 positions.
The bitwise OR operator is often used in situations where you want to set specific bits in a binary representation to 1, combining different sets of flags, or performing other bit-level manipulations in your code.