Java BigInteger Value Check isPowerOfTwo(BigInteger x)

Here you can find the source of isPowerOfTwo(BigInteger x)

Description

is Power Of Two

License

Open Source License

Declaration

public static boolean isPowerOfTwo(BigInteger x) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.math.BigInteger;

public class Main {
    public static boolean isPowerOfTwo(BigInteger x) {
        return x.signum() > 0 && x.getLowestSetBit() == x.bitLength() - 1;
    }//from  ww w . ja v a 2s.  co m

    /**
     * Returns {@code true} if {@code x} represents a power of two.
     *
     * <p>This differs from {@code Integer.bitCount(x) == 1}, because
     * {@code Integer.bitCount(Integer.MIN_VALUE) == 1}, but {@link Integer#MIN_VALUE} is not a power
     * of two.
     */
    public static boolean isPowerOfTwo(int x) {
        return x > 0 & (x & (x - 1)) == 0;
    }
}

Related

  1. isOdd(BigInteger in)
  2. isOdd(BigInteger x)
  3. isPerfectCubic(BigInteger n)
  4. isPositive(final BigInteger value)
  5. isPowerOfTwo(BigInteger bintValue)
  6. isRootInQuadraticResidues(BigInteger n, BigInteger p)
  7. isSqrtXXX(BigInteger n, BigInteger root)
  8. isZero(BigInteger value)