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.
Related
This question already has answers here:
What is the JavaScript >>> operator and how do you use it?
(7 answers)
How do I convert an integer to binary in JavaScript?
(17 answers)
Closed 5 years ago.
I found this code on W3schools for converting decimal into binary.
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
I don't understand what is the purpose for making zero fill right shift in this case? What is the use of setting the shift to 0 (i.e. dec >>> 0) since as I know it doesn't push any binary digit to the right at all?
I am a JS beginner. Thanks!
This question already has answers here:
What does ~~ ("double tilde") do in Javascript?
(12 answers)
Closed 7 years ago.
I was just messing with random stuff, while I found something interesting..
if I have ~ before a number, for example I have tried
~110100100 // result will be " -110100101 "
~11 // result will be " -12 "
is it making it negative and reducing it by 1? I don't have any idea, can anyone pleas explain this??
The operator ~ returns that result:
~N = -(N+1)
But this is an effect of inverting the value of all bits of a variable.
Double tilde ~~ is used to convert some types to int, since ~ operator converts the value to a 32-bit int before inverting its bits. Thus:
~~'-1' = -1
~~true = 1
~~false = 0
~~5.6 = 5
This question already has answers here:
What do these JavaScript bitwise operators do?
(3 answers)
Closed 8 years ago.
I've seen the following:
((2 * 45) + (2 * 124) + 100) >>> 3
Putting this in a console on its own reveals the value 54.
What is the purpose of >>> 3?
This is the Zero-fill right shift bitwise operator.
From the Mozilla Developer Network docs:
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.
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
This question already has answers here:
What does the ^ (caret) symbol do in JavaScript?
(5 answers)
Closed 6 years ago.
I thought that Math.pow(2,2) was equal to 2^2 but it is not. So what does ^ (caret) mean in JavaScript?
I was executing some tests in the console but didn't recognize the results:
2 ^ 2 = 0
2 ^ 3 = 1
1 ^ 2 = 3
It means bitwise XOR.
EDIT: Fixed link
It's a bitwise integer XOR operation (MDC link).
The ^ operator is bitwise XOR, you have more information in MDN:
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
That operator performs the logical XOR operation. (out bit is 1 when both input bits are different).
This is the bitwise XOR operator, which returns a one for each position where one (not both) of the corresponding bits of its operands is a one. The next example returns 4 (0100):
Code:
result = a ^ b;