diff --git a/src/main/java/org/apache/datasketches/common/Util.java b/src/main/java/org/apache/datasketches/common/Util.java index 35f8a9be1..729b92f3f 100644 --- a/src/main/java/org/apache/datasketches/common/Util.java +++ b/src/main/java/org/apache/datasketches/common/Util.java @@ -290,67 +290,45 @@ public static boolean isMultipleOf8AndGT0(final long v) { //Powers of 2 or powers of base related /** - * Returns true if given int argument is exactly a positive power of 2 and greater than zero. + * Returns true if given long argument is exactly a positive power of 2. * - * @param powerOf2 The input argument. - * @return true if argument is exactly a positive power of 2 and greater than zero. - */ - public static boolean isIntPowerOf2(final int powerOf2) { - return powerOf2 > 0 && (powerOf2 & powerOf2 - 1) == 0; //or (v > 0) && ((v & -v) == v) - } - - /** - * Returns true if given long argument is exactly a positive power of 2 and greater than zero. - * - * @param powerOf2 The input argument. - * @return true if argument is exactly a positive power of 2 and greater than zero. - */ - public static boolean isLongPowerOf2(final long powerOf2) { - return powerOf2 > 0 && (powerOf2 & powerOf2 - 1L) == 0; //or (v > 0) && ((v & -v) == v) - } - - /** - * Checks the given int argument to make sure it is a positive power of 2 and greater than zero. - * If not it throws an exception with the user supplied local argument name. - * @param powerOf2 The input int argument must be a power of 2 and greater than zero. - * @param argName Used in the thrown exception. - * @throws SketchesArgumentException if not a power of 2 nor greater than zero. + * @param n The input argument. + * @return true if argument is exactly a positive power of 2. */ - public static void checkIfIntPowerOf2(final int powerOf2, final String argName) { - if (isIntPowerOf2(powerOf2)) { return; } - throw new SketchesArgumentException("The value of the int argument \"" + argName + "\"" - + " must be a positive integer-power of 2" + " and greater than 0: " + powerOf2); + public static boolean isPowerOf2(final long n) { + return (n > 0) && ((n & (n - 1L)) == 0); //or (n > 0) && ((n & -n) == n) } /** - * Checks the given long argument to make sure it is a positive power of 2 and greater than zero. - * If not, it throws an exception with the user supplied local argument name. - * @param powerOf2 The input long argument must be a power of 2 and greater than zero. - * @param argName Used in the thrown exception. - * @throws SketchesArgumentException if not a power of 2 nor greater than zero. + * Checks the given long argument if it is a positive integer power of 2. + * If not, it throws an exception with the user supplied local argument name, if not null. + * @param n The input long argument must be a positive integer power of 2. + * @param argName Used in the thrown exception. It may be null. + * @throws SketchesArgumentException if not a positive integer power of 2. */ - public static void checkIfLongPowerOf2(final long powerOf2, final String argName) { - if (isLongPowerOf2(powerOf2)) { return; } - throw new SketchesArgumentException("The value of the int argument \"" + argName + "\"" - + " must be a positive integer-power of 2" + " and greater than 0: " + powerOf2); + public static void checkIfPowerOf2(final long n, String argName) { + if (isPowerOf2(n)) { return; } + argName = (argName == null) ? "" : argName; + throw new SketchesArgumentException("The value of the argument \"" + argName + "\"" + + " must be a positive integer power of 2: " + n); } /** * Computes the int ceiling power of 2 within the range [1, 2^30]. This is the smallest positive power - * of 2 that is equal to or greater than the given n and a mathematical integer. + * of 2 that is equal to or greater than the given n and a positive integer. * *

For: *

* * @param n The input int argument. * @return the ceiling power of 2. */ - public static int ceilingIntPowerOf2(final int n) { + public static int ceilingPowerOf2(final int n) { if (n <= 1) { return 1; } final int topIntPwrOf2 = 1 << 30; return n >= topIntPwrOf2 ? topIntPwrOf2 : Integer.highestOneBit(n - 1 << 1); @@ -358,20 +336,20 @@ public static int ceilingIntPowerOf2(final int n) { /** * Computes the long ceiling power of 2 within the range [1, 2^62]. This is the smallest positive power - * of 2 that is equal to or greater than the given n and a mathematical long. + * of 2 that is equal to or greater than the given n and a positive long. * *

For: *

* * @param n The input long argument. * @return the ceiling power of 2. */ - public static long ceilingLongPowerOf2(final long n) { + public static long ceilingPowerOf2(final long n) { if (n <= 1L) { return 1L; } final long topIntPwrOf2 = 1L << 62; return n >= topIntPwrOf2 ? topIntPwrOf2 : Long.highestOneBit(n - 1L << 1); @@ -380,7 +358,7 @@ public static long ceilingLongPowerOf2(final long n) { /** * Computes the floor power of 2 given n is in the range [1, 2^31-1]. * This is the largest positive power of 2 that equal to or less than the given n and equal - * to a mathematical integer. + * to a positive integer. * *

For: *