## What does ^ do in java?

**Moderators:** phlip, Moderators General, Prelates

### What does ^ do in java?

I've been trying to find a function for the upward angle bracket but cannot find one.

Anyone know what it does?

Anyone know what it does?

"I am a four hundred-foot tall purple Platypus Bear with pink horns and silver wings."

-Azula, Avatar: The Last Airbender.

-Azula, Avatar: The Last Airbender.

- phlip
- Restorer of Worlds
**Posts:**7572**Joined:**Sat Sep 23, 2006 3:56 am UTC**Location:**Australia-
**Contact:**

### Re: What does ^ do in java?

Specifically, a bitwise XOR. Have a look at that Wikipedia page for more details, and information about what it's used for.

Code: Select all

`enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};`

void ┻━┻︵╰(ಠ_ಠ ⚠) {exit((int)⚠);}

### Re: What does ^ do in java?

eaglef2 wrote:I've been trying to find a function for the upward angle bracket but cannot find one.

Anyone know what it does?

x^y = (x|y)&~(x&y)

- Mach1ne
**Posts:**35**Joined:**Tue Feb 24, 2009 5:20 pm UTC**Location:**This exact location but 3 minutes from now

### Re: What does ^ do in java?

Well whattaya know, learn something new everyday. Didn't know that.

### Re: What does ^ do in java?

Ahh... So that's why I missed those points on my CS2 test. I wrote ^ as a power operator. D:

### Re: What does ^ do in java?

phlip wrote:Specifically, a bitwise XOR

is there any other kind of XOR in Java? I use, and have seen

Code: Select all

`if ((truth1)^(truth2)) {...}`

and it seems to function perfectly

*/

### Re: What does ^ do in java?

fazzone wrote:phlip wrote:Specifically, a bitwise XOR

is there any other kind of XOR in Java? I use, and have seenCode: Select all

`if ((truth1)^(truth2)) {...}`

and it seems to function perfectly

If they're "1 if true, 0 if false", it'll work.

- Berengal
- Superabacus Mystic of the First Rank
**Posts:**2707**Joined:**Thu May 24, 2007 5:51 am UTC**Location:**Bergen, Norway-
**Contact:**

### Re: What does ^ do in java?

^ is defined on both integer types and booleans. On the integer types it's a bitwise xor, and on bools it's a boolean xor...

It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

### Re: What does ^ do in java?

Math.pow(double a, double b) is the power operation in Java (It returns a raised to the power of b).

(BTW, is there a builtin, integer-typed version of Math.pow, or should I just write my own as needed?)

(BTW, is there a builtin, integer-typed version of Math.pow, or should I just write my own as needed?)

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196

44288109756659334461284756482...

### Re: What does ^ do in java?

nerd65536 wrote:Math.pow(double a, double b) is the power operation in Java (It returns a raised to the power of b).

(BTW, is there a builtin, integer-typed version of Math.pow, or should I just write my own as needed?)

Other than (int)Math.pow, you mean?

### Re: What does ^ do in java?

I found one, as BigInteger.pow(int exponent)

Floating point arithmetic is both slower and less accurate than integer arithmetic.

(While double precision would suffice for (32-bit) integers, you lose precision if you want to work with long (64-bit) integers.)

Floating point arithmetic is both slower and less accurate than integer arithmetic.

(While double precision would suffice for (32-bit) integers, you lose precision if you want to work with long (64-bit) integers.)

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196

44288109756659334461284756482...

### Re: What does ^ do in java?

nerd65536 wrote:I found one, as BigInteger.pow(int exponent)

Floating point arithmetic is both slower and less accurate than integer arithmetic.

Math.pow is guaranteed to be accurate for integer inputs when the output can be represented as a double. And BigInteger is almost _certainly_ going to be much slower than float.

### Re: What does ^ do in java?

Random832 wrote:nerd65536 wrote:I found one, as BigInteger.pow(int exponent)

Floating point arithmetic is both slower and less accurate than integer arithmetic.

Math.pow is guaranteed to be accurate for integer inputs when the output can be represented as a double. And BigInteger is almost _certainly_ going to be much slower than float.

But he's still correct. Floating point is slower and less accurate.

You could always write your own efficient power function for integers and then compare it to Math's pow and BigInt's pow and pick to suit your needs.

- Why Two Kay
**Posts:**266**Joined:**Sun Mar 23, 2008 6:25 pm UTC**Location:**Plano, TX-
**Contact:**

### Re: What does ^ do in java?

Well that's the thing though. int arithmetic is fast, float is slower, but arbitrary precision such as BigInteger is always the slowest, unless it optimizes for values that fit in an int or long.

tl;dr - I said nothing important.

### Re: What does ^ do in java?

Floating point also has the advantage that it may be implemented in hardware in a way that only requires a few dozen instructions that can be inlined by the JIT - even a fast integer power function is going to depend on loops or recursion. There's always a trade-off of speed for accuracy, of course - the fact that Java's Math.pow guarantees integer accuracy is nice; it can be implemented whichever way's best to get that result.

Of course - depending on what power you want you're likely better off hardcoding x*x or x*x*x or int y=x*x;...y*y anyway

Of course - depending on what power you want you're likely better off hardcoding x*x or x*x*x or int y=x*x;...y*y anyway

- Xanthir
- My HERO!!!
**Posts:**5413**Joined:**Tue Feb 20, 2007 12:49 am UTC**Location:**The Googleplex-
**Contact:**

### Re: What does ^ do in java?

Random832 wrote:Of course - depending on what power you want you're likely better off hardcoding x*x or x*x*x or int y=x*x;...y*y anyway - even a fast integer power function is going to depend on loops or recursion.

Though a decent integer power function will do only log(n) multiplications, unlike the naive unrolled version.

(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

### Re: What does ^ do in java?

Xanthir wrote:Though a decent integer power function will do only log(n) multiplications, unlike the naive unrolled version.

That'd be the reason I said y=x*x;y*y for x

^{4}. Multiplications aren't the only expensive operation, though - comparisons and branches add up, and any general function is going to have the issue that it doesn't know what power it's trying to raise the number to and will need those.

- Xanthir
- My HERO!!!
**Posts:**5413**Joined:**Tue Feb 20, 2007 12:49 am UTC**Location:**The Googleplex-
**Contact:**

### Re: What does ^ do in java?

Oh, sorry, gotcha. I didn't understand your notation, and assumed you'd added a random semicolon and then mistyped x as y, twice.

(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

### Re: What does ^ do in java?

fazzone wrote:phlip wrote:Specifically, a bitwise XOR

is there any other kind of XOR in Java? I use, and have seenCode: Select all

`if ((truth1)^(truth2)) {...}`

and it seems to function perfectly

I was actually looking for the XOR in Java yesterday, and the internet correctly informed me that

Code: Select all

`if (boolean^boolean)`

Code: Select all

`if (boolean != boolean)`

I'm not sure if it actually compiles different, but still

### Who is online

Users browsing this forum: No registered users and 3 guests