How do I quickly calculate a large positive, or negative, power of 2 in Java?
问题 I want to calculate powers of two larger than 2 62 , so I must store the result in a double and can't use the (1L << exp) trick. I also want to store fractions representing negative powers of two. 回答1: Java provides java.lang.Math.scalb(float f, int scaleFactor) for this. It multiplies f by 2 scaleFactor . 回答2: Since the IEEE 754 standard specifies a hidden bit, you can simply leave the 52-bit significand portion as 0 and only need to change the exponent portion, which is a biased unsigned