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;
Related
This question already has an answer here:
How to have a number to be a power?
(1 answer)
Closed 3 years ago.
I'm trying to use ^ operator in javascript, like:
0.66666666666666666666666666666667^1.5
should equal
0,54433105395181735515495201660131
But when I'm trying to do it, it won't return me anything, it will return either 67,0 or 1, depending on my attempts.
Desired Output
console.log(100 * 0.66666666666666666666666666666667^1.5);
How do I solve this problem?
You need to take the exponenciation operator **instead of bitwise XOR ^.
Alternate use the classing Math.pow.
console.log(0.66666666666666666666666666666667 ** 1.5)
^ means XOR in JavaScript. If you want exponentiation, you have to use Math.pow or ** instead.
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.
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 do these JavaScript bitwise operators do?
(3 answers)
Closed 9 years ago.
I am trying to figure out this operator on JS -
'string' ^= 'string';
But I can not find ant information.
Is that a comparison or assignment ?
Thanks.
myVar ^= 5 is the same as myVar = myVar ^ 5. ^ is the bitwise xor operator
Let's say myVar was set to 2
5 in binary is: 101
2 in binary is: 010
Exclusive "or" checks the first bit of both numbers and sees 1,0 and returns 1 then sees 0,1 and returns 1 and sees 1,0 and returns 1.
Thus 111 which converted back to decimal is 7
So 5^2 is 7
var myVar = 2;
myVar ^= 5;
alert(myVar); // 7
^ (caret) is the Bitwise XOR (Exclusive Or) operator. As with more common operator combinations like +=, a ^= b is equivalent to a = a^b.
See the Javascript documentation from Mozilla for more details.
x ^= y is bitwise XOR and shorthand for x = x^y - and so is technically an "assignment" to answer your question. And to be precise the single operator '^' indicates the bitwise XOR.
As d'alar'cop (and several others by now) already pointed out, this means something called XOR. I always hate to read a wikipedia explanation, so I'm going to put another explanation here:
'XOR' means 'eXclusive OR'. What is that? First an example:
11000110 -- random byte
10010100
--------- ^ -- XOR
01010010
XOR is some bitwise operation, returning two if one of two bits is 1 and the other 0. If they're both 1, it's 'and', not 'exclusive or' ('normal or' would allow two 1's).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what's the difference between ( | ) and ( || ) in javascript?
I've seen this in a couple examples here but I never fully understood what it's supposed to do. Can anyone give me a simple example please?
In Javascript, the | operator is a bitwise operators (in contrast to the || operator which is a logical operator).
It convert each operand to a 32-bit number, and performs a bitwise or between them.
Example of expressions and their results:
1 | 1 === 1
1 | 2 === 3
1.99 | 2.99 === 3
Reference: http://developer.mozilla.org/en/JavaScript/Reference/operators/bitwise_operators
Depends where you use it:
In regular expression it represents "or": /[a-z]|[0-9]/i
It is also bitwise or operator, as described in: What does the "|" (single pipe) do in JavaScript?