What does the | operator do? [duplicate] - javascript

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?

Related

How to use operator "^" in JavaScript? [duplicate]

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.

Why 10.333333 | 0 = 10 in JavaScript? [duplicate]

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.

Why doesn't AngularJS allow for xor operations? [duplicate]

This question already has an answer here:
How to use xor functionality in angular js expression? [closed]
(1 answer)
Closed 6 years ago.
<form name="businessFormAbout" ng-show="(businessFormAbout.$submitted)^(businessForm.$submitted)">
This line shows error in console:
[$parse:lexerr] Lexer Error: Unexpected next character at columns 30-30 [^] in expression [(businessFormAbout.$submitted)^(businessForm.$submitted)]
It's not AngularJS that doesn't support XOR: JavaScript itself doesn't support XOR as you might define it.
JS does have a bitwise XOR operator (^), but this only works for numbers.
You could create your own XOR function a bit like this:
function XOR(a,b) {
return ( a || b ) && !( a && b );
}

What is the meaning of ^= operator in JS [duplicate]

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).

What does the caret symbol (^) do in JavaScript? [duplicate]

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;

Categories