15 Jun 2024C++ / Python / TypeScriptEasy

Power of Two

Collected C++, Python, TypeScript solutions for power of two. Add a dedicated write-up later if you want deeper notes.

auto-generated entry for Power of Two. the solution files are available below.

solution files

  • C++ power-of-two/synced-solution.cpp
  • Python power-of-two/synced-solution.py
  • TypeScript power-of-two/synced-solution.ts

Solution files

Pythonpower-of-two/synced-solution.py
def isPowerOfTwo(n: int) -> bool:
    return n > class="syntax-number">0 and (n & (n - class="syntax-number">1)) == class="syntax-number">0
C++power-of-two/synced-solution.cpp
class Solution {
public:
    bool isPowerOfTwo(int n) {
        return n > class="syntax-number">0 && (n & (n - class="syntax-number">1)) == class="syntax-number">0;
    }
};
TypeScriptpower-of-two/synced-solution.ts
function isPowerOfTwo(n: number): boolean {
    return n > class="syntax-number">0 && (n & (n - class="syntax-number">1)) === class="syntax-number">0;
}