
Problem Statement
Given an integer n
, the task is to determine if n
can be expressed as two raised to some integer power. In mathematical terms, this is stated as checking if there exists an integer x
such that n
equals 2^x
. The function should return true if n
is a power of two and false otherwise. Understanding whether a number can be represented as a power of two is fundamental in various areas of computer science, particularly when dealing with binary computations.
Examples
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
Approach and Intuition
Understanding the problem involves recognizing that powers of two have a unique property in binary representation: they are represented as a single '1' followed by any number of '0's. This peculiar characteristic allows us to devise multiple methods to solve the problem efficiently. The constraints (-2^31 <= n <= 2^31 - 1) guide us on the valid range of input values and clarify that negative numbers and zero cannot be powers of two.
Examples
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
Steps to Approach:
- Initial Check for Edge Cases:
First and foremost, if
n
is less than or equal to zero, it cannot be a power of two. Thus, we return false immediately.
- Example:
- For
n = 0
: Directly returnfalse
as zero is not a power of two.
- For
- Binary Representation and Bitwise Operations:
Utilize the characteristic property of powers of two in their binary form (only one bit is '1', and all others are '0'). By using a bitwise AND operation between
n
andn-1
, ifn
is a power of two, the result should be zero. This is because subtracting one from a power of two flips all the bits after the last '1' bit.
- Example:
- Consider
n = 16
which is10000
in binary.n-1
equals15
, which is01111
in binary. The bitwise AND of10000
(16) and01111
(15) results in00000
, hence returningtrue
.
- Consider
- Mathematical Logarithmic Check:
Another method is to use logarithms to verify if the logarithm base two of
n
gives an integer. This approach, however, may involve floating-point computations which can be prone to precision errors.
Thus, by applying these methods, we can efficiently determine if a given number n
is a power of two. The utilization of bitwise operations especially leverages lower-level characteristics of numbers making this check very efficient with a constant time complexity.
Solutions
- C++
- Java
- C
- Python
class Solution {
public:
bool checkPowerOfTwo(int num) {
if (num == 0) return false;
long number = num;
return (number & (number - 1)) == 0;
}
};
This solution determines if a given integer is a power of two using bitwise operations in C++. The function checkPowerOfTwo
takes an integer num
and performs the following steps:
- First, check if the number is zero. If
num
is zero, the function immediately returnsfalse
because zero is not a power of two. - The integer is cast to a
long
type to handle any potential issues with larger integers or negative numbers that could arise from subtractive operations between signed and unsigned. - Use a bitwise AND operation between
number
andnumber - 1
. In binary terms, a power of two has exactly one bit set to 1. By subtracting 1 from the number, all bits after the set bit (including the-bit-that-was-set) flip. The bitwise AND of these two numbers will be zero only if there was exactly one bit set in the original number, confirming it's a power of two. - The result of the bitwise operation is compared to zero. If it equals zero, the function returns
true
, indicating the number is a power of two; otherwise, it returnsfalse
.
class Solution {
public boolean checkPowerOfTwo(int num) {
if (num == 0) return false;
long convertedNum = (long) num;
return (convertedNum & (convertedNum - 1)) == 0;
}
}
The provided Java solution determines if a given integer num
is a power of two. The function checkPowerOfTwo(int num)
returns false
immediately if num
is zero, addressing the edge case where zero is not a power of two. For non-zero integers, the solution uses a bit manipulation trick.
By converting the number to a long
type to handle larger integer values safely, the function checks if there is only one bit set in the binary representation of the number (which is characteristic of powers of two). It performs a bitwise AND operation between num
and num - 1
. If the result is zero, then num
is a power of two; otherwise, it is not. This method is efficient, operating with a time complexity of O(1) due to the constant time bit manipulation operation.
bool checkIfPowerOfTwo(int number) {
if (number == 0) return false;
long value = number;
return (value & (value - 1)) == 0;
}
In this guide, you learn how to determine if a number is a power of two using C. The provided checkIfPowerOfTwo
function takes an integer (number
) and returns a boolean indicating whether the number is a power of two.
The function first checks if the number is zero. Since zero is not a power of two, it returns false
immediately if this condition is met. Next, it converts the integer into a long integer (value
) to handle any potential issues with larger numbers or negative values.
The core of the function utilizes a bitwise AND operation, value & (value - 1)
. This operation is a common trick to determine if a number is a power of two. This expression will be zero if number
is a power of two, and non-zero otherwise.
For example, for power of two numbers like 2, 4, 8, etc., the binary representation is solely one '1' followed by '0's. Subtracting 1 from such a number flips all the bits after the '1' (including the '1' itself), resulting in a number where none of the bits match the original number. Performing an AND operation between the original number and the number minus one results in zero.
Summarily, the function returns true
if (value & (value - 1)) == 0
evaluates to true, signifying the number is a power of two, and false
otherwise. This single-line check offers an efficient and concise method to solve the problem.
class Solution(object):
def checkPowerOfTwo(self, number: int) -> bool:
if number == 0:
return False
return number & (number - 1) == 0
This summary discusses a Python solution for determining if a given number is a power of two. The function checkPowerOfTwo
in the Solution
class takes an integer parameter, number
, and returns a boolean value.
- If the input
number
is zero, return False directly as zero is not a power of two. - For any other integer, use the bitwise operation:
number & (number - 1)
. If a number is indeed a power of two, this operation results in zero which confirms the condition. Thus, return True when the result is zero, False otherwise.
The efficiency of this approach lies in the use of bitwise operations that are generally faster than arithmetic operations, providing a quick and effective solution to check the power of two condition.
No comments yet.