What does ^ do in java?

A place to discuss the science of computers and programs, from algorithms to computability.

Formal proofs preferred.

Moderators: phlip, Moderators General, Prelates

User avatar
eaglef2
Posts: 93
Joined: Mon Feb 09, 2009 4:52 am UTC
Location: I am over there

What does ^ do in java?

Postby eaglef2 » Mon Mar 09, 2009 12:05 am UTC

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

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.

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: What does ^ do in java?

Postby fazzone » Mon Mar 09, 2009 1:25 am UTC

XOR
*/

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: What does ^ do in java?

Postby phlip » Mon Mar 09, 2009 1:34 am UTC

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)⚠);}
[he/him/his]

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: What does ^ do in java?

Postby Random832 » Mon Mar 09, 2009 2:50 am UTC

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)

User avatar
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?

Postby Mach1ne » Tue Mar 10, 2009 4:12 pm UTC

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

VDOgamez
Posts: 122
Joined: Tue May 06, 2008 1:49 am UTC

Re: What does ^ do in java?

Postby VDOgamez » Wed Mar 11, 2009 2:06 pm UTC

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

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: What does ^ do in java?

Postby fazzone » Wed Mar 11, 2009 7:33 pm UTC

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
*/

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: What does ^ do in java?

Postby Random832 » Wed Mar 11, 2009 7:40 pm UTC

fazzone wrote:
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

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

User avatar
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?

Postby Berengal » Wed Mar 11, 2009 7:58 pm UTC

^ 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.

User avatar
nerd65536
Posts: 39
Joined: Wed Jul 19, 2006 10:49 pm UTC

Re: What does ^ do in java?

Postby nerd65536 » Wed Mar 11, 2009 10:23 pm UTC

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?)

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196
44288109756659334461284756482...

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: What does ^ do in java?

Postby Random832 » Wed Mar 11, 2009 11:18 pm UTC

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?

User avatar
nerd65536
Posts: 39
Joined: Wed Jul 19, 2006 10:49 pm UTC

Re: What does ^ do in java?

Postby nerd65536 » Thu Mar 12, 2009 2:04 am UTC

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.)

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196
44288109756659334461284756482...

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: What does ^ do in java?

Postby Random832 » Thu Mar 12, 2009 4:19 am UTC

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.

Soljer
Posts: 29
Joined: Fri Feb 27, 2009 6:31 pm UTC

Re: What does ^ do in java?

Postby Soljer » Thu Mar 12, 2009 4:14 pm UTC

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.

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

Re: What does ^ do in java?

Postby Why Two Kay » Thu Mar 12, 2009 11:45 pm UTC

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.

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: What does ^ do in java?

Postby Random832 » Fri Mar 13, 2009 1:54 pm UTC

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

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: What does ^ do in java?

Postby Xanthir » Fri Mar 13, 2009 2:00 pm UTC

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)))

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: What does ^ do in java?

Postby Random832 » Fri Mar 13, 2009 2:06 pm UTC

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 x4. 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.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: What does ^ do in java?

Postby Xanthir » Fri Mar 13, 2009 2:13 pm UTC

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)))

Jeroen B
Posts: 21
Joined: Tue Dec 02, 2008 9:43 am UTC

Re: What does ^ do in java?

Postby Jeroen B » Thu Mar 26, 2009 11:15 am UTC

fazzone wrote:
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

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

Code: Select all

if (boolean^boolean)
might just as well be written as

Code: Select all

if (boolean != boolean)
which for some reason I really like :P

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


Return to “Computer Science”

Who is online

Users browsing this forum: No registered users and 6 guests