Add PowerOfTwoOrNotTest (#4279)

This commit is contained in:
Piotr Idzik
2023-08-04 14:31:47 +02:00
committed by GitHub
parent ee23b6c2e5
commit c4a9ef1566
2 changed files with 28 additions and 9 deletions

View File

@ -4,14 +4,8 @@ package com.thealgorithms.maths;
* A utility to check if a given number is power of two or not. For example 8,16
* etc.
*/
public class PowerOfTwoOrNot {
public static void main(String[] args) {
assert !checkIfPowerOfTwoOrNot(0);
assert checkIfPowerOfTwoOrNot(1);
assert checkIfPowerOfTwoOrNot(8);
assert checkIfPowerOfTwoOrNot(16);
assert checkIfPowerOfTwoOrNot(1024);
public final class PowerOfTwoOrNot {
private PowerOfTwoOrNot() {
}
/**
@ -21,7 +15,7 @@ public class PowerOfTwoOrNot {
* @return {@code true} if given number is power of two, otherwise
* {@code false}
*/
public static boolean checkIfPowerOfTwoOrNot(int number) {
public static boolean checkIfPowerOfTwoOrNot(final int number) {
return number != 0 && ((number & (number - 1)) == 0);
}
}