How does the expression "float | 0" produces an integer? [duplicate] - javascript

This question already has answers here:
Using bitwise OR 0 to floor a number
(7 answers)
Closed 8 years ago.
Please describe how does 5.55 | 0 produces 5 in JavaScript. I want to know what is happening in this bitwise operating. Thanks!

The bitwise operators in Javascript automatically coerce their arguments to 32-bit integer values by dropping the fraction and any high-order bits beyond 32. So
5.55 | 0
is treated like:
5 | 0

The operands of bitwise operations are always converted to signed 32-bit integers in big-endian order and in two's complement format.
That would be
00000000000000000000000000000101
or 00000000000000000000000000000000
------------------------------------
00000000000000000000000000000101

Related

Bitwise operations on large numbers [duplicate]

This question already has an answer here:
Bitwise OR operation with 0b transform a given number in negative
(1 answer)
Closed 7 months ago.
This works as expected:
> 0b1111
15
> 0b1111 & 0b1111
15
But this doesn't:
> 0b11111111111111111111111111111111
4294967295
> 0b11111111111111111111111111111111 & 0b11111111111111111111111111111111
-1
What's going on here? And how can I safely perform bitwise operations on large (up to 2^32) numbers in Javascript?
Update: This is below Number.MAX_SAFE_INTEGER so I don't believe there should be a loss of precision.
What's going on here?
JavaScript's bitwise operators will interpret the result as a signed 32-bit number (one exception is the >>> operator), but apart from that, the result is correct.
And how can I safely perform bitwise operations on large (up to 2^32) numbers in Javascript?
Apply >>> 0 to the result so the result is interpreted as an unsigned 32 bit number:
let res = (0b11111111111111111111111111111111
& 0b11111111111111111111111111111111) >>> 0;
console.log(res);

Different calculation result [duplicate]

This question already has answers here:
Bitshift in javascript
(4 answers)
Closed 1 year ago.
Explain to me why the result of the following calculation is different:
Python:
tmp = 4235635936 << 0 #4235635936
JS
let tmp = 4235635936 << 0 // -59331360
Question - how do I get the same result in python as in js ?
The binary representation for both numbers is the same. One is signed, the other is unsigned.
>>> struct.pack("<i", -59331360)
b'\xe0\xacv\xfc'
>>> struct.pack("<I", 4235635936)
b'\xe0\xacv\xfc'
>>>
in JS, the bitwise operators and shift operators operate on 32-bit ints, and your example overflows the 32-bit capacity.
In Python there's no capacity limit for integers, so you get the actual value.

Why 10.333333 | 0 = 10 in JavaScript? [duplicate]

This question already has answers here:
What does the "|" (single pipe) do in JavaScript?
(5 answers)
Closed 4 years ago.
I'm wondering how JavaScript evaluates the following expression:
10.333333 | 0 === 10
Is it because of bitwise ORing ignores the decimal part?
JavaScript bitwise operators all work by converting their operands to 32-bit integers. The operation is performed and the result is converted back to a (floating point) number.

What does `>>>` mean in JavaScript? [duplicate]

This question already has answers here:
What is the JavaScript >>> operator and how do you use it?
(7 answers)
Closed 5 years ago.
What is the meaning of the expression >>> in JavaScript? It is like type conversion, or what, and when it recommended to use?
I ran into that symbol (>>>) when I read this article and am a little confused.
Sorry, if my question is stupid, but I can not find any answers by Google search or other ways.
>>> is a bitwise operator.
>>> (Zero-fill right shift) This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the
right are discarded. Zero bits are shifted in from the left. The sign
bit becomes 0, so the result is always non-negative.
For non-negative numbers, zero-fill right shift and sign-propagating
right shift yield the same result. For example, 9 >>> 2 yields 2, the
same as 9 >> 2
From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

~ in javascript [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to: The ~ operator?
What's the ~ operator does in javascript?
input
alert(~1)
output is
-2
input
~function () {}()
output is
-1
I never heard about ~ operator in javascript
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
Bitwise NOT ~ a Inverts the bits of its operand.
I guess its fairly odd that a function returns -1, but what would you expect anyway.
This is the bitwise not operator that inverts the value of every bit in the integer. In binary a signed integer has the following representation:
00000001 = 1
11111110 = -2
See this wikipedia article.
The bitwise NOT operator (~) will take its operand, convert it to a
32-bit integer, and will invert each bit so that each 0 becomes a 1
and vice versa.
http://james.padolsey.com/javascript/double-bitwise-not/

Categories