https://github.com/shaldonbarnes10/power_of_two
This repository contains a solution to the Power of Two problem, where the goal is to determine if a given integer is a power of two. The problem is solved using efficient bitwise operations in C++.
https://github.com/shaldonbarnes10/power_of_two
bitwise-operators boolean cpp
Last synced: 4 months ago
JSON representation
This repository contains a solution to the Power of Two problem, where the goal is to determine if a given integer is a power of two. The problem is solved using efficient bitwise operations in C++.
- Host: GitHub
- URL: https://github.com/shaldonbarnes10/power_of_two
- Owner: Shaldonbarnes10
- Created: 2025-01-13T14:03:39.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-01-13T14:13:42.000Z (6 months ago)
- Last Synced: 2025-01-13T15:24:13.258Z (6 months ago)
- Topics: bitwise-operators, boolean, cpp
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Power of Two
This repository contains a solution to the **Power of Two** problem, where the goal is to determine if a given integer is a power of two. The problem is solved using efficient bitwise operations in C++.
## Problem Description
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 = 2^x
\]### Example Input and Output
| Input | Output | Explanation |
|--------|--------|------------------------|
| `n = 1` | `true` | \( 2^0 = 1 \) |
| `n = 16` | `true` | \( 2^4 = 16 \) |
| `n = 3` | `false` | 3 is not a power of two |---
## Constraints
- \( -2^{31} \leq n \leq 2^{31} - 1 \)
---
## Solution
### Approach
The solution leverages the following properties of numbers that are powers of two:
1. A power of two has exactly one bit set in its binary representation.
2. The bitwise operation \( n \& (n - 1) = 0 \) holds true only for powers of two.### Algorithm
1. If `n <= 0`, return `false` (negative numbers and zero are not powers of two).
2. Use the condition \( n \& (n - 1) == 0 \) to determine if `n` is a power of two.---
## Implementation
### C++ Solution
```cpp
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
};
```---
## Usage
### Input
Provide an integer \( n \) as input.
### Output
The function returns `true` if the input is a power of two; otherwise, it returns `false`.
---
## Test Cases
| Test Case | Expected Output |
|-----------|-----------------|
| `1` | `true` |
| `16` | `true` |
| `3` | `false` |
| `0` | `false` |
| `-2` | `false` |---
## Complexity
### Time Complexity
- \( O(1) \): The bitwise operation is performed in constant time.
### Space Complexity
- \( O(1) \): No extra space is used.
---
## License
This repository is available under the MIT License.