mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
22 lines
561 B
Java
22 lines
561 B
Java
package com.thealgorithms.maths;
|
|
|
|
/**
|
|
* A utility to check if a given number is power of two or not. For example 8,16
|
|
* etc.
|
|
*/
|
|
public final class PowerOfTwoOrNot {
|
|
private PowerOfTwoOrNot() {
|
|
}
|
|
|
|
/**
|
|
* Checks whether given number is power of two or not.
|
|
*
|
|
* @param number the number to check
|
|
* @return {@code true} if given number is power of two, otherwise
|
|
* {@code false}
|
|
*/
|
|
public static boolean checkIfPowerOfTwoOrNot(final int number) {
|
|
return number != 0 && ((number & (number - 1)) == 0);
|
|
}
|
|
}
|