adammenges
adammenges

Reputation: 8868

Java Error - What am I doing wrong with this exponential?

Alright so I've got this piece of code:

blah = (26^0)*(1);
System.out.println(blah);

Which produces the output 26, when it should be equal to 1. What am I doing wrong? What can I do to fix this?

Upvotes: 1

Views: 3844

Answers (4)

Ben
Ben

Reputation: 663

As the previous responses said you are actually doing a bitwise XOR (which results in 26) and then multiplying by 1. See Bitwise and Bit Shift Operators and Summary of Operators for more info. You should be using Math.pow(base, exponent) so Math.pow(26.0, 0.0) as described in the Math api

Upvotes: 0

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27271

Math.pow(base, exponent) works. The ^ means Bitwise-XOR.

So, you should use:

blah = Math.pow(26, 0) * 1;
System.out.println(blah);

Upvotes: 1

Sam Dufel
Sam Dufel

Reputation: 17608

I think you're confusing the ^ operator. In Java, the ^ operator does an exclusive-or operation. To get a power, you need to use Math.pow(a,b)

Upvotes: 15

helloworld922
helloworld922

Reputation: 10939

In Java, the operator ^ is not exponentiate, but rather bitwise-xor. Anything xor 0 is itself, so 26^0=26, 26*1=26

Upvotes: 3

Related Questions