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
Related
This question already has an answer here:
ECMAScript 6 negative binary literal
(1 answer)
Closed 3 years ago.
So 0b1 is 1. How to write -1 in binary format?
The docs says that the leftmost bit is reserved for sign, but where is that leftmost bit? Even 0b11111111111111111111111111111111 is still a positive number. I naively tried 1b with no success of course.
try
let a = -0b1;
console.log(a);
This question already has answers here:
Why does OR 0 round numbers in Javascript?
(3 answers)
Closed 5 years ago.
The Mozilla JavaScript Guide has this to say about the bitwise OR operator:
"Bitwise OR: Returns a zero in each bit position for which the corresponding bits of both operands are zeros."
However, when operand b is 1, the bitwise operator OR in the example below rounds up a. This is something I cannot wrap my head around. Also, seeing as a returns a non rounded number, it is also not clear to me how the bitwise operator truncates it down to two digits.
var a = Math.random()*100;
console.log(a);
console.log(a | 1);
Insightful explanations are very welcome.
The hidden factor here is bitwise operators cast the number to an integer before being applied. That is why the number gets rounded. The cast truncates any fractional part.
The bitwise OR itself then simply sets the first bit to 1. So may or may not increase the truncated number by 1 depending on whether it was odd or even after the truncation.
So it's not rounding it up in all cases - just 50% of the time.
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:
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;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What do these operators do?
I'm working with some javascript for html 5's canvas. I'm looking at some existing code and I've come across the following:
element.height >> 1
element.width >> 1
Its used as part of some arithmetic.
I am using prototype.js as well, if this helps.
>> is the bitwise shifting operator. So >> 1 basically shifts the binary representation of the number on the left by one to the right. This is equal to an integer division by 2.
So element.height >> 1 equals to Math.floor( element.height / 2)
It's a bitshift operator.
It's a sign-propagating right-shift; full explanation here: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators#.3E.3E_%28Sign-propagating_right_shift%29.
Shifting (bitshift) operator.
1 << 1
This shifts the bitpattern 00000001 to the left once (padding with 0s)- you get 00000010, which is 2.
1 << 2
shifts it by two, so you get 00000100, which is 4.
It comes useful when implementing binary protocols, where only 2 bits can mean something. Using shifting you can strip out the rest.
This is the sign-propagating right shift operator which shifts the digits of the binary representation of the first operand to the right by the number of places specified by the second operand, discarding any shifted off to the right. The copies of the leftmost bit are added on from the left, thereby preserving the sign of the number.
So in your case everything is shifted one place to the right.