Java BigInteger Value Check isPowerOfTwo(BigInteger bintValue)

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

Description

is Power Of Two

License

Open Source License

Declaration

public static boolean isPowerOfTwo(BigInteger bintValue) 

Method Source Code


//package com.java2s;
import java.math.BigInteger;

public class Main {
    public static boolean isPowerOfTwo(int intValue) {
        if (intValue == 0) {
            return false;
        }// w  w w. jav  a  2  s.  c om

        while ((intValue & 1) == 0) {
            intValue = intValue >>> 1;
        }

        return intValue == 1;
    }

    public static boolean isPowerOfTwo(long longValue) {
        if (longValue == 0L) {
            return false;
        }

        while ((longValue & 1L) == 0L) {
            longValue = longValue >>> 1;
        }

        return longValue == 1L;
    }

    public static boolean isPowerOfTwo(BigInteger bintValue) {
        int bitIndex = bintValue.getLowestSetBit();

        if (bitIndex < 0) {
            return false;
        }

        return bintValue.clearBit(bitIndex).equals(BigInteger.ZERO);
    }
}

Related

  1. isNumberInRange(String text, BigInteger min, BigInteger max)
  2. isOdd(BigInteger in)
  3. isOdd(BigInteger x)
  4. isPerfectCubic(BigInteger n)
  5. isPositive(final BigInteger value)
  6. isPowerOfTwo(BigInteger x)
  7. isRootInQuadraticResidues(BigInteger n, BigInteger p)
  8. isSqrtXXX(BigInteger n, BigInteger root)
  9. isZero(BigInteger value)