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.
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 an answer here:
Javascript integer beginning with zero [duplicate]
(1 answer)
Closed 5 years ago.
I have created a calculator but it has such problem
var x=060;
var y=60;
console.log(x+y);
The output is 108 why so?
And, How can i make calculator to enter single operator and wait for an operand and then an operator could appear in.
This is my calculator
Calculator2.0
Put 0 in front of a number means the base is octal. In your case 060 (octal) = 48 (decimal). You add 60 + 48 that is equal to 108.
This question already has answers here:
What does % do in JavaScript?
(10 answers)
Closed 7 years ago.
When I was repairing a Project via Java that I did about a year ago I came across a piece of Code using n%7 and its a mystery to me what it does. What I understand what it exactly means - Not referring to %.
Thanks in advance,
DR
It is called modulus. It can be read as n modulus 7.
The MODULUS Operator %
The modulus operator finds the modulus of its first operand with respect to the second. That is, it produces the remainder of dividing the first value by the second value. For example:
22 % 6 = 4 because 22 / 6 = 3 with a remainder of 4
read more here: http://mathbits.com/MathBits/Java/DataBasics/Mathoperators.htm
This question already has an answer here:
Why does alert("1" - - "1"); produce 2 in javascript?
(1 answer)
Closed 8 years ago.
Why does alert("1" - - "1") return 2?
I'm not entirely sure what is going on here to create the result as 2?
I came across the problem here:
http://davidshariff.com/js-quiz
it's like writing 1 - (-1) which is 1 + 1 = 2
the problem in js is that if you use + between 2 string it means concating them thus resulting 11
EDIT: thanks to iamnotmaynard comment i was able to find this post
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
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.