PDA

View Full Version : ^ operator

gizmo1650
08-25-2010, 08:56 PM
i was using my JavaScript console as a calculator, and i accidentally used "^" for exponents.
the statement did not return a syntax error, but a number, after doing some more ^ operations i can't see the relation between the numbers i use and the answer it gives me.

qwertyuiop
08-25-2010, 09:10 PM
Googling "javascript caret operator" returns this (http://www.java-samples.com/showtutorial.php?tutorialid=820).

Fou-Lu
08-25-2010, 09:28 PM
Its simply a bit XOR.
It returns a result where each bit in the left side that matches on the right side is inverted. Just on a nibble calculation since I don't want to type all the 0's.
5 = 0101
4 = 0100

5 ^ 4 = 1
The 'leftmost' bit in the above (2^(3-1), where this one is actually the exponent) is 1 in both the 5 and the 4, so its inverted to 0. The rightmost one (2^(1-1)) is 1 only in the 5 and not in the 4, so it remains untouched. Simple as that.

Dang, beaten. That's a good link too, all its missing is the not (since I don't think derivatives like NAND exist in most languages), which is simply a unary ~.