Why | (operator or binary) truncates a decimal number?


Good morning. My question is quite simple, I show it with this example:

$('#resultado').append($('<p>').html(13.135345 | 0));
$('#resultado').append($('<p>').html(122.333 | 0));
$('#resultado').append($('<p>').html(56.43 | 0));
$('#resultado').append($('<p>').html(NaN | 0));
$('#resultado').append($('<p>').html('1233.5555' | 0));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <p>Ejemplo de truncamiento con operador O binario</p>

<section id="resultado"></section>

Why when using the operator | in decimal numbers do you truncate them? I saw this in the following SO question in

asked by UselesssCat 17.11.2017 в 15:37

1 answer


Bit-level operators treat their operands as a 32-bit sequence (ones and zeros). Any number is passed to whole in 32-bit add-on and then operated.

You can find more details at MDN or in the official specification of < a href="http://www.ecma-international.org/ecma-262/6.0/#sec-binary-bitwise-operators-runtime-semantics-evaluation"> ECMAScript

answered by 17.11.2017 / 15:57