Bird
0
0
DSA Cprogramming~20 mins

Check if Number is Power of Two in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Power of Two Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code snippet?
Given the function to check if a number is a power of two, what will be printed for n = 16?
DSA C
int isPowerOfTwo(unsigned int n) {
    return n && !(n & (n - 1));
}

#include <stdio.h>

int main() {
    unsigned int n = 16;
    if (isPowerOfTwo(n)) {
        printf("Yes\n");
    } else {
        printf("No\n");
    }
    return 0;
}
ANo
B1
C0
DYes
Attempts:
2 left
💡 Hint
A power of two has exactly one bit set in binary.
Predict Output
intermediate
2:00remaining
What is the output when n = 0?
Using the same function, what will be printed for n = 0?
DSA C
int isPowerOfTwo(unsigned int n) {
    return n && !(n & (n - 1));
}

#include <stdio.h>

int main() {
    unsigned int n = 0;
    if (isPowerOfTwo(n)) {
        printf("Yes\n");
    } else {
        printf("No\n");
    }
    return 0;
}
AYes
B1
CNo
D0
Attempts:
2 left
💡 Hint
Zero is not a power of two.
🔧 Debug
advanced
2:00remaining
What error does this code raise?
Identify the error in this code snippet that attempts to check if n is a power of two.
DSA C
int isPowerOfTwo(unsigned int n) {
    return n & (n - 1) == 0;
}

#include <stdio.h>

int main() {
    unsigned int n = 8;
    if (isPowerOfTwo(n)) {
        printf("Yes\n");
    } else {
        printf("No\n");
    }
    return 0;
}
ALogical error causing wrong output
BAlways prints 'No'
CNo error, prints 'Yes'
DSyntax error
Attempts:
2 left
💡 Hint
Operator precedence matters in bitwise and comparison operations.
🧠 Conceptual
advanced
1:00remaining
How many numbers between 1 and 16 (inclusive) are powers of two?
Count how many numbers in the range 1 to 16 are powers of two.
A5
B4
C6
D7
Attempts:
2 left
💡 Hint
Powers of two are 1, 2, 4, 8, 16 in this range.
🚀 Application
expert
3:00remaining
What is the value of variable 'count' after this code runs?
This code counts how many numbers in an array are powers of two. What is the final count?
DSA C
#include <stdio.h>

int isPowerOfTwo(unsigned int n) {
    return n && !(n & (n - 1));
}

int main() {
    unsigned int arr[] = {3, 4, 6, 8, 16, 18, 32};
    int count = 0;
    for (int i = 0; i < 7; i++) {
        if (isPowerOfTwo(arr[i])) {
            count++;
        }
    }
    printf("%d\n", count);
    return 0;
}
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Check each number if it has exactly one bit set.