views:

652

answers:

13

What function does the "^" operator serve in Java?

When I try this:

int a = 5^n;

...it gives me:

for n = 5, returns 0
for n = 4, returns 1
for n = 6, returns 3

...so I guess it doesn't indicate exponentiation. But what is it then?

+5  A: 

It is the XOR bitwise operator.

AraK
+12  A: 

It's bitwise XOR, Java does not have an exponentiation operator, you would have to use Math.pow() instead.

Dan Dyer
+4  A: 

As others have said, it's bitwise XOR. If you want to raise a number to a given power, use Math.pow.

Jon Skeet
+4  A: 

As already stated by the other answer(s), it's the "exclusive or" (XOR) operator. For more information on bit-operators in Java, see: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html

Bart Kiers
+3  A: 

AraK's link points to the definition of exclusive-or, which explains how this function works for two boolean values.

The missing piece of information is how this applies to two integers (or integer-type values). Bitwise exclusive-or is applied to pairs of corresponding binary digits in two numbers, and the results are re-assembled into an integer result.

To use your example:

  • The binary representation of 5 is 0101.
  • The binary representation of 4 is 0100.

A simple way to define bitwise XOR is to say the result has a 1 in every place where the two input numbers differ.

With 4 and 5, the only difference is in the last place; so

0101 ^ 0100 = 0001 (5 ^ 4 = 1) .

Carl Smotricz
+19  A: 

As many people have already pointed out, it's the XOR operator. Many people have also already pointed out that if you want exponentiation then you need to use Math.pow.

But I think it's also useful to note that ^ is just one of a family of operators that are collectively known as bitwise operators:

Operator    Name         Example     Result  Description
a & b       and          3 & 5       1       1 if both bits are 1.
a | b       or           3 | 5       7       1 if either bit is 1.
a ^ b       xor          3 ^ 5       6       1 if both bits are different.
~a          not          ~3          -4      Inverts the bits.
n << p      left shift   3 <<< 2     12      Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions.
n >> p      right shift  5 >> 2      1       Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions.
n >>> p     right shift  -4 >>> 28   15      Shifts the bits of n right p positions. Zeros are shifted into the high-order positions.

From here.

These operators can come in handy when you need to read and write to integers where the individual bits should be interpreted as flags, or when a specific range of bits in an integer have a special meaning and you want to extract only those. You can do a lot of every day programming without ever needing to use these operators, but if you ever have to work with data at the bit level, a good knowledge of these operators is invaluable.

Mark Byers
+1  A: 

It is the bitwise xor operator in java which results 1 for different value (ie 1 ^ 0 = 1) and 0 for same value (ie 0 ^ 0 = 0).

GK
A: 

Thank you all, very helpfull.

joroj
You should accept an answer joroj.
Shervin
+43  A: 
polygenelubricants
just to add to this answer, the function the OP is looking for is probably Math.pow(10, 1)
tjohns20
Oops didn't realize that, thanks for answering.
Simon
+5  A: 

That is because you are using the xor operator.

In java, or just about any other language, ^ is bitwise xor, so of course,

10 ^ 1 = 11. more info about bitwise operators

It's interesting how Java and C# don't have a power operator.

jcao219
+1  A: 

In other languages like Python you can do 10**2=100, try it.

mkotechno
+1  A: 

^ is binary (as in base-2) xor, not exponentiation (which is not available as a Java operator). For exponentiation, see java.lang.Math.pow().

SHiNKiROU