English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a javascript snippet:
var z=(x^y); //x and y are previously defined integers
I thought ^ this would be x to the power of y, but, the calculations don't seem to work. Sample output:
x=80 y=108 x^y=60
x=31 y=120 x^y=103
x=26 y=103 x^y=125
x=10 y=107 x^y=97
x=30 y=99 x^y=125

If it were x to the power of y, these would all be HUGE numbers.

2006-12-11 08:39:33 · 5 answers · asked by Miles Libbey 2 in Computers & Internet Programming & Design

5 answers

It's bitwise XOR, so only bits that are 1 in one of the numbers and 0 in the other will be set to 0.
so to take a couple of your examples:
80 = 0101 0000
108 = 0110 1100
XOR = 0011 1100 = 0x3C = 60

31 = 0001 1111
120 = 0111 1000
XOR = 0110 0111 = 0x67 = 103

2006-12-11 09:09:33 · answer #1 · answered by TankAnswer 4 · 1 0

XOR is one of the foundations of boolean math...

all those boolean expressions can be transformed from one set of operators to others!

a XOR b is the same as (a or b) and not (a and b)

but much more concise, it is usually implemented by a hardware operator, so it is very fast.

and javascript call XOR ^.

2006-12-11 09:59:14 · answer #2 · answered by jake cigar™ is retired 7 · 1 0

^ is Bitwise XOR - Returns a one in each bit position for which the corresponding bits of either but not both operands are ones.

to do powers, do Math.pow(x,y)

2006-12-11 09:35:09 · answer #3 · answered by Anonymous · 1 0

^ in programming languages means bitwise XOR.

2006-12-11 08:42:22 · answer #4 · answered by csanon 6 · 1 0

^ is a bitwise exlcusive OR.. don't ask
I believe Javascript uses the Pow(x,y) function or Exp(x,y)

2006-12-11 08:43:38 · answer #5 · answered by shadowkat 5 · 1 0

fedest.com, questions and answers