简体   繁体   English

Java中的pow(x,y)

[英]pow (x,y) in Java

What's the difference between: 有什么区别:

Math.pow ( x,y ); // x^y

To: 至:

x^y; // x^y

?

Will I prefer to use x^y with double type numbers? 我是否更喜欢使用带有double数字的x^y Or shell I have to use always with Math.pow() method? 或shell我必须始终使用Math.pow()方法?

^ is the bitwise exclusive OR (XOR) operator in Java (and many other languages). ^是Java(以及许多其他语言)中的按位异或 (XOR)运算符。 It is not used for exponentiation. 它不用于取幂。 For that, you must use Math.pow . 为此,您必须使用Math.pow

Additionally for what was said, if you want integer powers of two, then 1 << x (or 1L << x ) is a faster way to calculate 2 x than Math.pow(2,x) or a multiplication loop, and is guaranteed to give you an int (or long ) result. 另外对于所说的,如果你想要2的整数幂,那么1 << x (或1L << x )是比Math.pow(2,x)或乘法循环计算2 x的更快的方法,并且是保证给你一个int (或long )结果。

It only uses the lowest 5 (or 6) bits of x (ie x & 31 (or x & 63 )), though, shifting between 0 and 31 (or 63) bits. 它仅使用x的最低5(或6)位(即x & 31 (或x & 63 )),但在0和31(或63)位之间移位。

在Java x ^ y是一个XOR操作。

x^y is not " x to the power of y ". x^y不是“ xy的幂”。 It's " x XOR y " . 这是x XOR y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM