-3
    for(int i = 0; i < n; i++) {
        num += digits[i]*(10^(n-1-i));
        System.out.println(10^(n-1-i));
    }

My purpose is to change a array representation of a number into Integer representation. For example, [9,9] is 9*(10^1) + 9*(10^0) = 90+9 = 99. However, the output of 10^(n-1-i) is:

11
10

Is anything wrong with my code, or there is the other way to operate "^"? Thank you.

Julia
  • 1
  • 1
  • `^` is not raising into power, `Math.pow` – Dmitry Bychenko Dec 16 '17 at 22:19
  • @Aominè despite the title, that question is actually asking about a logical xor, not bitwise. But I'm sure there's a question out there for bitwise... – yshavit Dec 16 '17 at 22:19
  • You're looking for Math.pow as in Math.pow(10, (n-1-i)). ^ is bitwise XOR, don't worry about it for now. –  Dec 16 '17 at 22:20
  • Please do some **research**. It's as simple as a **web search** for [`java operators`](https://www.google.com/search?q=java+operators), which will lead you to pages like "[The Java™ Tutorials - Operators](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html)", that clearly show `^` as being **bitwise exclusive OR**. – Andreas Dec 16 '17 at 22:23

2 Answers2

1

Operator ^ is a XOR operation, as specified by the Java specification. Java does not provide an operator that implements raising to a power. Either use Math.pow() or code your own function to do that.

OLEGSHA
  • 388
  • 3
  • 13
0

^ is xor, not raising into power, which is Math.pow; you can try to get rid of power at all:

int power = 1;

for (int i = n - 1; i >= 0; i--) {
    num += digits[i] * power;

    power *= 10; 
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215