Javascript - Operator Bitwise OR

Introduction

The bitwise OR operator is | and works on two numbers.

Bitwise OR follows the following truth table:

Bit From First Number Bit From Second Number Result
1 1 1
1 0 1
0 1 1
0 0 0

A bitwise OR operation returns 1 if at least one bit is 1. It returns 0 only if both bits are 0.

The code below uses OR on the numbers 25 and 3:

var result = 25 | 3; 
console.log(result);       //27 

Here is how it worked.

25 = 0000 0000 0000 0000 0000 0000 0001 1001 
 3 = 0000 0000 0000 0000 0000 0000 0000 0011 
--------------------------------------------- 
OR = 0000 0000 0000 0000 0000 0000 0001 1011 

Related Topic