console.log(0.5 | 0); // 0
console.log(-1 | 0); // -1
console.log(1 | 0); // 1
Why does 0.5 | 0
return zero, but any integer (including negative) returns the input integer? What does the single pipe ("|") do?
console.log(0.5 | 0); // 0
console.log(-1 | 0); // -1
console.log(1 | 0); // 1
Why does 0.5 | 0
return zero, but any integer (including negative) returns the input integer? What does the single pipe ("|") do?
This example will help you.
This is a bitwise or.
Since bitwise operations only make sense on integers,
0.5
is truncated.0 | x
isx
, for anyx
.A single pipe is a bit-wise OR.
JavaScript truncates any non-integer numbers in bitwise operations, so its computed as
0|0
, which is 0.Bit comparison is so simple it's almost incomprehensible ;) Check out this "nybble"
Bitwise ORing 6 and 10 will give you 14:
Terribly confusing!