+ 1

What is the difference between ^ and Math.pow()

The exponential symbol can also be used for the same sake as the Math.pow func is used. Is there any difference between them and what one should prefer

20th Jul 2019, 9:53 AM
Ayush Pandey
Ayush Pandey - avatar
4 Respuestas
+ 14
^ is used as bitwise XOR operator in java, so you can't use it for raising a number to some power. lesson on XOR operator: https://www.sololearn.com/learn/4074/?ref=app
20th Jul 2019, 10:35 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
I guess in java math.pow() only works, ^ and .^ are usually used in c++, more precisely, in Matlab, there should no be difference as long as both functions to calculate the power of an integer
20th Jul 2019, 10:21 AM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 3
in Java ^ operator is for XOR (exclusive OR) bitwise operation int a = 0b1100; // 12 dec int b = 0b1010; // 10 dec System.out.println( Integer.toBinaryString( a ^ b ) ); // 0110 bin, 6 dec
20th Jul 2019, 11:04 AM
zemiak
0
The diferrence If you use Math.pow() you still need call the class and the method of Math class. If use ^ maybe it use to giving easily way to power a number
20th Jul 2019, 10:19 AM
Dwi Novianto Nugroho
Dwi Novianto Nugroho - avatar