@@ -290,88 +290,66 @@ public static boolean isMultipleOf8AndGT0(final long v) {
290
290
//Powers of 2 or powers of base related
291
291
292
292
/**
293
- * Returns true if given int argument is exactly a positive power of 2 and greater than zero .
293
+ * Returns true if given long argument is exactly a positive power of 2.
294
294
*
295
- * @param powerOf2 The input argument.
296
- * @return true if argument is exactly a positive power of 2 and greater than zero.
297
- */
298
- public static boolean isIntPowerOf2 (final int powerOf2 ) {
299
- return powerOf2 > 0 && (powerOf2 & powerOf2 - 1 ) == 0 ; //or (v > 0) && ((v & -v) == v)
300
- }
301
-
302
- /**
303
- * Returns true if given long argument is exactly a positive power of 2 and greater than zero.
304
- *
305
- * @param powerOf2 The input argument.
306
- * @return true if argument is exactly a positive power of 2 and greater than zero.
307
- */
308
- public static boolean isLongPowerOf2 (final long powerOf2 ) {
309
- return powerOf2 > 0 && (powerOf2 & powerOf2 - 1L ) == 0 ; //or (v > 0) && ((v & -v) == v)
310
- }
311
-
312
- /**
313
- * Checks the given int argument to make sure it is a positive power of 2 and greater than zero.
314
- * If not it throws an exception with the user supplied local argument name.
315
- * @param powerOf2 The input int argument must be a power of 2 and greater than zero.
316
- * @param argName Used in the thrown exception.
317
- * @throws SketchesArgumentException if not a power of 2 nor greater than zero.
295
+ * @param n The input argument.
296
+ * @return true if argument is exactly a positive power of 2.
318
297
*/
319
- public static void checkIfIntPowerOf2 (final int powerOf2 , final String argName ) {
320
- if (isIntPowerOf2 (powerOf2 )) { return ; }
321
- throw new SketchesArgumentException ("The value of the int argument \" " + argName + "\" "
322
- + " must be a positive integer-power of 2" + " and greater than 0: " + powerOf2 );
298
+ public static boolean isPowerOf2 (final long n ) {
299
+ return (n > 0 ) && ((n & (n - 1L )) == 0 ); //or (n > 0) && ((n & -n) == n)
323
300
}
324
301
325
302
/**
326
- * Checks the given long argument to make sure it is a positive power of 2 and greater than zero .
327
- * If not, it throws an exception with the user supplied local argument name.
328
- * @param powerOf2 The input long argument must be a power of 2 and greater than zero .
329
- * @param argName Used in the thrown exception.
330
- * @throws SketchesArgumentException if not a power of 2 nor greater than zero .
303
+ * Checks the given long argument if it is a positive integer power of 2.
304
+ * If not, it throws an exception with the user supplied local argument name, if not null .
305
+ * @param n The input long argument must be a positive integer power of 2.
306
+ * @param argName Used in the thrown exception. It may be null.
307
+ * @throws SketchesArgumentException if not a positive integer power of 2.
331
308
*/
332
- public static void checkIfLongPowerOf2 (final long powerOf2 , final String argName ) {
333
- if (isLongPowerOf2 (powerOf2 )) { return ; }
334
- throw new SketchesArgumentException ("The value of the int argument \" " + argName + "\" "
335
- + " must be a positive integer-power of 2" + " and greater than 0: " + powerOf2 );
309
+ public static void checkIfPowerOf2 (final long n , String argName ) {
310
+ if (isPowerOf2 (n )) { return ; }
311
+ argName = (argName == null ) ? "" : argName ;
312
+ throw new SketchesArgumentException ("The value of the argument \" " + argName + "\" "
313
+ + " must be a positive integer power of 2: " + n );
336
314
}
337
315
338
316
/**
339
317
* Computes the int ceiling power of 2 within the range [1, 2^30]. This is the smallest positive power
340
- * of 2 that is equal to or greater than the given n and a mathematical integer.
318
+ * of 2 that is equal to or greater than the given n and a positive integer.
341
319
*
342
320
* <p>For:
343
321
* <ul>
344
322
* <li>n ≤ 1: returns 1</li>
345
323
* <li>2^30 ≤ n ≤ 2^31 -1 : returns 2^30</li>
346
324
* <li>n == an exact power of 2 : returns n</li>
347
- * <li>otherwise returns the smallest power of 2 ≥ n and equal to a mathematical integer</li>
325
+ * <li>otherwise returns the smallest power of 2 ≥ n and equal to a positive integer</li>
348
326
* </ul>
349
327
*
350
328
* @param n The input int argument.
351
329
* @return the ceiling power of 2.
352
330
*/
353
- public static int ceilingIntPowerOf2 (final int n ) {
331
+ public static int ceilingPowerOf2 (final int n ) {
354
332
if (n <= 1 ) { return 1 ; }
355
333
final int topIntPwrOf2 = 1 << 30 ;
356
334
return n >= topIntPwrOf2 ? topIntPwrOf2 : Integer .highestOneBit (n - 1 << 1 );
357
335
}
358
336
359
337
/**
360
338
* Computes the long ceiling power of 2 within the range [1, 2^62]. This is the smallest positive power
361
- * of 2 that is equal to or greater than the given n and a mathematical long.
339
+ * of 2 that is equal to or greater than the given n and a positive long.
362
340
*
363
341
* <p>For:
364
342
* <ul>
365
343
* <li>n ≤ 1: returns 1</li>
366
344
* <li>2^62 ≤ n ≤ 2^63 -1 : returns 2^62</li>
367
345
* <li>n == an exact power of 2 : returns n</li>
368
- * <li>otherwise returns the smallest power of 2 ≥ n and equal to a mathematical integer </li>
346
+ * <li>otherwise returns the smallest power of 2 ≥ n and equal to a positive long </li>
369
347
* </ul>
370
348
*
371
349
* @param n The input long argument.
372
350
* @return the ceiling power of 2.
373
351
*/
374
- public static long ceilingLongPowerOf2 (final long n ) {
352
+ public static long ceilingPowerOf2 (final long n ) {
375
353
if (n <= 1L ) { return 1L ; }
376
354
final long topIntPwrOf2 = 1L << 62 ;
377
355
return n >= topIntPwrOf2 ? topIntPwrOf2 : Long .highestOneBit (n - 1L << 1 );
@@ -380,7 +358,7 @@ public static long ceilingLongPowerOf2(final long n) {
380
358
/**
381
359
* Computes the floor power of 2 given <i>n</i> is in the range [1, 2^31-1].
382
360
* This is the largest positive power of 2 that equal to or less than the given n and equal
383
- * to a mathematical integer.
361
+ * to a positive integer.
384
362
*
385
363
* <p>For:
386
364
* <ul>
@@ -402,7 +380,7 @@ public static int floorPowerOf2(final int n) {
402
380
/**
403
381
* Computes the floor power of 2 given <i>n</i> is in the range [1, 2^63-1].
404
382
* This is the largest positive power of 2 that is equal to or less than the given <i>n</i> and
405
- * equal to a mathematical integer.
383
+ * equal to a positive integer.
406
384
*
407
385
* <p>For:
408
386
* <ul>
@@ -534,7 +512,7 @@ public static double powerSeriesNextDouble(final int ppb, final double curPoint,
534
512
* Returns the ceiling of a given <i>n</i> given a <i>base</i>, where the ceiling is an integral power of the base.
535
513
* This is the smallest positive power of <i>base</i> that is equal to or greater than the given <i>n</i>
536
514
* and equal to a mathematical integer.
537
- * The result of this function is consistent with {@link #ceilingIntPowerOf2 (int)} for values
515
+ * The result of this function is consistent with {@link #ceilingPowerOf2 (int)} for values
538
516
* less than one. I.e., if <i>n < 1,</i> the result is 1.
539
517
*
540
518
* <p>The formula is: <i>base<sup>ceiling(log<sub>base</sub>(x))</sup></i></p>
@@ -622,7 +600,7 @@ public static int numberOfLeadingOnes(final long v) {
622
600
* @throws SketchesArgumentException if not a power of 2 nor greater than zero.
623
601
*/
624
602
public static int exactLog2OfInt (final int powerOf2 , final String argName ) {
625
- checkIfIntPowerOf2 (powerOf2 , argName );
603
+ checkIfPowerOf2 (powerOf2 , argName );
626
604
return Integer .numberOfTrailingZeros (powerOf2 );
627
605
}
628
606
@@ -635,7 +613,7 @@ public static int exactLog2OfInt(final int powerOf2, final String argName) {
635
613
* @throws SketchesArgumentException if not a power of 2 nor greater than zero.
636
614
*/
637
615
public static int exactLog2OfLong (final long powerOf2 , final String argName ) {
638
- checkIfLongPowerOf2 (powerOf2 , argName );
616
+ checkIfPowerOf2 (powerOf2 , argName );
639
617
return Long .numberOfTrailingZeros (powerOf2 );
640
618
}
641
619
@@ -646,7 +624,7 @@ public static int exactLog2OfLong(final long powerOf2, final String argName) {
646
624
* @return the log2 of the given int value if it is an exact power of 2 and greater than zero.
647
625
*/
648
626
public static int exactLog2OfInt (final int powerOf2 ) {
649
- if (!isIntPowerOf2 (powerOf2 )) {
627
+ if (!isPowerOf2 (powerOf2 )) {
650
628
throw new SketchesArgumentException ("Argument 'powerOf2' must be a positive power of 2." );
651
629
}
652
630
return Long .numberOfTrailingZeros (powerOf2 );
@@ -659,7 +637,7 @@ public static int exactLog2OfInt(final int powerOf2) {
659
637
* @return the log2 of the given long value if it is an exact power of 2 and greater than zero.
660
638
*/
661
639
public static int exactLog2OfLong (final long powerOf2 ) {
662
- if (!isLongPowerOf2 (powerOf2 )) {
640
+ if (!isPowerOf2 (powerOf2 )) {
663
641
throw new SketchesArgumentException ("Argument 'powerOf2' must be a positive power of 2." );
664
642
}
665
643
return Long .numberOfTrailingZeros (powerOf2 );
0 commit comments