Given an integer n, return true if it is a power of two. Otherwise, return false.

An integer n is a power of two, if there exists an integer x such that n == 2x.

Example 1:

Input: n = 1 Output: true Explanation: 20 = 1

Example 2:

Input: n = 16 Output: true Explanation: 24 = 16

Example 3:

Input: n = 3 Output: false

Constraints:

  • -231 <= n <= 231 - 1

Follow up: Could you solve it without loops/recursion?


Note: This problem is from LeetCode.
โœ… Compiled
โœ… Executed
โš ๏ธ Correct
program main
    implicit none

    integer :: n
    logical :: result

    ! Example 1:
    n = 1
    result = is_power_of_two(n)
    print '(L1)', result

    ! Example 2:
    n = 16
    result = is_power_of_two(n)
    print '(L1)', result

    ! Example 3:
    n = 3
    result = is_power_of_two(n)
    print '(L1)', result

contains

    function is_power_of_two(n) result(result)
        implicit none
        integer, intent(in) :: n
        logical :: result
        integer :: x

        result = .false.
        do x = 1, n
            if (n == 2 * x) then
                result = .true.
                exit
            end if
        end do
    end function is_power_of_two

end program main
โŒ Compiled
โŒ Executed
โŒ Correct
module power_of_two
implicit none
private
public :: is_power_of_two
interface is_power_of_two
module procedure is_power_of_two_int
end interface
contains
logical function is_power_of_two_int(n) result(is_power_of_two)
integer, intent(in) :: n
is_power_of_two = (n > 0).and.(n.eq.2**floor(log(real(n,kind=8))/log(2.0)))
end function is_power_of_two_int
end module power_of_two

program power_of_two_test
use power_of_two
implicit none
integer :: n
logical :: is_power_of_two
n = 1
is_power_of_two = is_power_of_two_int(n)
if (is_power_of_two) then
    print *, "The number ", n, " is a power of two."
else
    print *, "The number ", n, " is not a power of two."
end if

n = 16
is_power_of_two = is_power_of_two_int(n)
if (is_power_of_two) then
    print *, "The number ", n, " is a power of two."
else
    print *, "The number ", n, " is not a power of two."
end if

n = 3
is_power_of_two = is_power_of_two_int(n)
if (is_power_of_two) then
    print *, "The number ", n, " is a power of two."
else
    print *, "The number ", n, " is not a power of two."
end if
end program power_of_two_test
๐ŸŒ Data from online sources
def is_power_of_two(n):
    if n <= 0:
        return False
    return (n & (n - 1)) == 0

The algorithm checks if the given integer n is a power of two. First, we check if n is a negative number or zero, in which case it cannot be a power of two, so we return false.

If n is positive, we can use bitwise manipulation to check if it's a power of two. A power of two in binary representation has only one bit set to 1 (e.g., 1: 001, 2: 010, 4: 100, 8: 1000). When we subtract 1 from a power of two, all the bits from the right of the set bit become 1 (e.g., 1-1: 000, 2-1: 001, 4-1: 011, 8-1: 0111).

By performing bitwise AND operation (n & (n-1)) between n and (n-1), we can check if there is only one bit set in n. If the result is 0, that means there is no overlapping bit between n and (n-1), indicating that n is a power of two; returning true. Otherwise, return false.

๐ŸŒ Data from online sources
bool isPowerOfTwo(int n) {
    if (n <= 0) {
        return false;
    }
    return (n & (n - 1)) == 0;
}

The algorithm checks if the given integer n is a power of two. First, we check if n is a negative number or zero, in which case it cannot be a power of two, so we return false.

If n is positive, we can use bitwise manipulation to check if it's a power of two. A power of two in binary representation has only one bit set to 1 (e.g., 1: 001, 2: 010, 4: 100, 8: 1000). When we subtract 1 from a power of two, all the bits from the right of the set bit become 1 (e.g., 1-1: 000, 2-1: 001, 4-1: 011, 8-1: 0111).

By performing bitwise AND operation (n & (n-1)) between n and (n-1), we can check if there is only one bit set in n. If the result is 0, that means there is no overlapping bit between n and (n-1), indicating that n is a power of two; returning true. Otherwise, return false.