This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the JavaScript >>> operator and how do you use it?
I was browsing through some documentation on MDN for the indexOf workaround (link) and I came across this line of code:
var len = t.length >>> 0;
Was just curious what that did, as I've never seen it.
Answer is in the first comment.
Unsigned right shift. See Unsigned Right Shift Operator (>>>) (JavaScript)
.
Related
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
This question already has answers here:
What's the difference between & and && in JavaScript?
(4 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 6 years ago.
I am looking through the source of a library I'm working with, and I found something I haven't seen before.
$(item).each(function(child) {
oddEven = (i & 1);
targetNode.append(jasper_build_product(this,oddEven));
i++;
});
Notice the oddEven = (i & 1);. What does the (i & 1) part do? I'm particularly curious about the ampersand.
The & operator is a bitwise AND, and more specifically the expression x & 1 returns the least significant bit (LSB) of the value x.
Since the internal representation of numbers is base-2, an LSB of 1 indicates an odd value, and 0 indicates an even value.
This question already has answers here:
What does a tilde do when it precedes an expression?
(5 answers)
Closed 8 years ago.
I am not a JavaScript expert, but I'm reading the code and found out that there's a statement like this.
if(!~dssClass.indexOf("hideDiv")
What's the "~" mean in this statement?
Thanks
It's bitwise NOT: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
"take the result of the indexOf lookup, bitwise invert it, then take the logical NOT of that"
This question already has answers here:
What does a tilde do when it precedes an expression?
(5 answers)
What exactly does ~ do? [duplicate]
(3 answers)
Closed 8 years ago.
I'm using and adapting the MVC example included in the Express.js repository.
In one of the modules, there's a JavaScript construct I'm not familiar with. The intent is to iterate through keys on an object and skip a few that are "reserved", but I don't understand what is happening with the tilde from a JavaScript perspective.
for (var key in obj) {
if (~['name', 'prefix', 'engine', 'before'].indexOf(key)) continue;
}
I'm reading it's a Bitwise NOT operator, but would appreciate an explanation in layman's terms of what that means, as well as what it's doing in this particular example.
indexOf returns -1 if not found. ~-1 is 0, i.e. false. So the whole things means: if key is found in the array, do continue.
This question already has answers here:
What do these JavaScript bitwise operators do?
(3 answers)
Closed 8 years ago.
I'm translating a JavaScript algorithm into PHP, and I ran into the symbol >>, and I have no clue what it means. It's hard to search Google for symbols, so can anyone tell me what it means?
It's a bit shifting operator:
http://www.contactor.se/~dast/fpl-old/language/shift.HTML
It is a sign-propagating right shift. Many, many, languages have this operator.
Wikipedia has a good article on the subject. My first link has a few examples and an explanation.
Other answers are correct, but this may be of help to you: If x is positive then
x >> y
is the same as
floor(x / (2 ** y))
where 2**y is 2 raised to the power y.
E.g. x >> 3 is the same as floor(x / 8).
Bitwise right shift
Looks like PHP has this operator too:
http://us2.php.net/operators.bitwise