35. Check If a Number Is a Power of Two

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>
#include <stdint.h>

// Complete the function
const char* is_power_of_two(uint32_t n) {
    // A power of two must be greater than 0
    // And (n & (n - 1)) must be 0
    if (n > 0 && (n & (n - 1)) == 0) {
        return "YES";
    }
    return "NO";
}

int main() {
    uint32_t n;
    if (scanf("%u", &n) != 1) return 0;

    const char* result = is_power_of_two(n);
    printf("%s", result);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote