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).
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:
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 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:
Javascript conditional statement with a single pipe "|"
(4 answers)
Closed 7 years ago.
Does is matter whether I use | or || as an or operator? Both seem to work interchangeably.
function Sum(num, sum) {
sum = sum | 0;
return sum;
}
function SumII(num, sum) {
sum = sum || 0;
return sum;
}
console.log(Sum(7));
//0
console.log(SumII(7));
//0
Yes, there is a huge difference.
The operator | is the bitwise operator or.
I cite the linked documentation:
Returns a one in each bit position for which the corresponding bits of either or both operands are ones.
So, by using this operator with two numbers will give you another number that is built from the first twos.
The operator || is the logical operator or.
It evaluates its operands as booleans (imagine as an implicit cast take place if needed) and it returns the first true value in its original form.
It happens that for certain operations the result is the same (as an example, if used in a guard, where the result of the applied operator is treated as a boolean value), no matter which operator you decide to use, but it would be better to know what are the differences between them.
Logical operations
Use || and && instead of | and & because the first ones use a "short circuit" mechanism. So, they are more efficient (and safe) because if the left term was already evaluated to true / false, the result is known without evaluating the last terms.
E.g.: (for an undefined x)
true | x // ReferenceError: x is not defined
true || x // true
false & x // ReferenceError: x is not defined
false && x // false
Bits operations
Use | and &.
E.g.:
4|2 returns 6
4 = 100b
2 = 010b
6 = 110b
(see the OR operation on every column: 1 OR 0 = 1, 0 OR 1 = 1, 0 OR 0 = 0)
4||2 returns 4
4 is a "true" value (because it is not 0,false,null,"", undefined or NaN) => the result doesn't depend on the bits values => the operator is not bitwise
They are not the same
| is BITWISE OR operator. It takes the binary value of the two operands and compares each bit using OR logic.
|| is the LOGICAL OR operator. It checks the truthy-ness of each operand and compares those using OR logic.
Try for example
x = 42 || 65 // X now equals 42
x = 42 | 65 // X now equals 107 - 42 bitwise ORed to 65
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;