Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ^ operator do in Java?

What function does the ^ (caret) 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 perform exponentiation. But what is it then?

like image 338
joroj Avatar asked Jan 02 '10 11:01

joroj


People also ask

What is << in Java?

Left shift operator shifts the bits of the number towards left a specified number of positions. The symbol for this operator is <<.

What is the use of && and || operator in Java?

&& is used to perform and operation means if anyone of the expression/condition evaluates to false whole thing is false. || is used to perform or operation if anyone of the expression/condition evaluates to true whole thing becomes true. so it continues till the end to check atleast one condition to become true.


1 Answers

The ^ operator in Java

^ in Java is the exclusive-or ("xor") operator.

Let's take 5^6 as example:

(decimal)    (binary)      5     =  101      6     =  110 ------------------ xor      3     =  011 

This the truth table for bitwise (JLS 15.22.1) and logical (JLS 15.22.2) xor:

^ | 0 1      ^ | F T --+-----     --+----- 0 | 0 1      F | F T 1 | 1 0      T | T F 

More simply, you can also think of xor as "this or that, but not both!".

See also

  • Wikipedia: exclusive-or

Exponentiation in Java

As for integer exponentiation, unfortunately Java does not have such an operator. You can use double Math.pow(double, double) (casting the result to int if necessary).

You can also use the traditional bit-shifting trick to compute some powers of two. That is, (1L << k) is two to the k-th power for k=0..63.

See also

  • Wikipedia: Arithmetic shift

Merge note: this answer was merged from another question where the intention was to use exponentiation to convert a string "8675309" to int without using Integer.parseInt as a programming exercise (^ denotes exponentiation from now on). The OP's intention was to compute 8*10^6 + 6*10^5 + 7*10^4 + 5*10^3 + 3*10^2 + 0*10^1 + 9*10^0 = 8675309; the next part of this answer addresses that exponentiation is not necessary for this task.

Horner's scheme

Addressing your specific need, you actually don't need to compute various powers of 10. You can use what is called the Horner's scheme, which is not only simple but also efficient.

Since you're doing this as a personal exercise, I won't give the Java code, but here's the main idea:

8675309 = 8*10^6 + 6*10^5 + 7*10^4 + 5*10^3 + 3*10^2 + 0*10^1 + 9*10^0         = (((((8*10 + 6)*10 + 7)*10 + 5)*10 + 3)*10 + 0)*10 + 9 

It may look complicated at first, but it really isn't. You basically read the digits left to right, and you multiply your result so far by 10 before adding the next digit.

In table form:

step   result  digit  result*10+digit    1   init=0      8                8    2        8      6               86    3       86      7              867    4      867      5             8675    5     8675      3            86753    6    86753      0           867530    7   867530      9          8675309=final 
like image 84
polygenelubricants Avatar answered Sep 29 '22 05:09

polygenelubricants