October 15, 1999 - The Bitwise OR Operator | WebReference

October 15, 1999 - The Bitwise OR Operator

Yehuda Shiran October 15, 1999
The Bitwise OR Operator
Tips: October 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

The bitwise OR operator compares two bits and assign 1 to the result when at least one of the operands is 1. Here is the truth table for this operator:

Bit1Bit2Bit1 | Bit2
000
011
101
111

The OR operator, like all other bitwise operators, can take only numeric values as its operands. Since JavaScript does not support bit-length variables, you have to do your bitwise math with byte-length operands, i.e. with integers that are less than 256. Any operand longer than a byte (greater than 255) will be chopped and only the LSB (Least Significant Byte) will be used.

Let's do a simple calculation:

NameDecimalHexBinary
Op1700x4601000110
Op21210x7901111001
Op1 | Op21270x7F01111111