All submissions

Check If a Number Is a Power of Two

Code

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

int is_power_of_two(uint32_t n) {
    
    if (n == 0) {
        return 0;
    }
    // The bitwise operation checks if only one bit is set.
    return (n & (n - 1)) == 0;
}

int main() {
    uint32_t n;
    scanf("%u", &n);

    
    if (is_power_of_two(n)) {
        printf("YES\n");
    } else {
        printf("NO\n");
    }

    return 0;
}

Solving Approach

 

 

 

Loading...

Input

8

Expected Output

YES