-1

I was recently looking at some other questions and doing a looking around and came across someone using x^y (x to the power of y).

Unfortunately this doesn't work. Here are a few examples and their outputs.

2^2 // 0
2^3 // 1
2^4 // 6

Math.pow(2,2) // 4
Math.pow(2,3) // 8
Math.pow(2,4) // 16

Why doesn't num^power work? I swear it used to. What arithmetic is it actually doing?

Spedwards
  • 4,167
  • 16
  • 49
  • 106

1 Answers1

3

^ is a logical, bitwise operator for the XOR operation.

XOR is an operation which for any bit at a given position, results 1 (true) if this bit has different values for two compared variables (and 0 otherwise).

Here are your three examples in binary:

    10 (2)          10 (2)         010 (2)
XOR 10 (2)      XOR 11 (3)     XOR 100 (4)
   ---             ---            ----
    00 (0)          01 (1)         110 (6)
kamituel
  • 34,606
  • 6
  • 81
  • 98