|

expr bit-wise "or" operator, dual of &

Arguments must be integers, result is an integer.

Bit n of the result is 0 if bit n of each argument is 0. Otherwise, bit n of the result is 1.

For negative arguments, observe that ~$n==(-1-n) (See the ~ operator for further discussion.) The following cases then exist:

Case Result
$a>=0,$b>=0 $a|$b, as defined above.
$a>=0,$b<0 $a|$b == ~(~$a & ~$b) De Morgan's Law
$a|$b == ~(~$a & (-1-$b)) Extended definition of ~
$a|$b == -1-(~$a & (-1-$b)) Extended definition of ~
The expression (-1-$b) is nonnegative, and so the expression (~$a & (-1-$b)) can be evaluated by bitwise operations.
$a<0,$b>=0 Commute to ($b | $a) and solve as above.
$a<0,$b<0 $a|$b == ~(~$a & ~$b) De Morgan's Law
$a|$b == -1-((-1-$a) & (-1-$b)) Extended definition of ~
Since (-1-$a) and (-1-$b) are both nonnegative, the & in the expression above can be evaluated wuth bitwise operations.

For logical/short-cut "or" use the || operator.


AMG: Like &, | has lower precedence than the comparison operators. See & for discussion, examples, and references.