Bird
0
0
DSA Cprogramming~10 mins

Check if Number is Power of Two in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a number is a power of two using bitwise AND.

DSA C
int isPowerOfTwo(unsigned int n) {
    return n && !(n & (n [1] 1));
}
Drag options to blanks, or click blank then click option'
A+
B-
C<<
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or shift operators instead of subtraction.
Not checking if n is zero before the bitwise operation.
2fill in blank
medium

Complete the code to return 1 if n is power of two, else 0.

DSA C
int isPowerOfTwo(unsigned int n) {
    if (n == 0) return 0;
    return (n & (n [1] 1)) == 0;
}
Drag options to blanks, or click blank then click option'
A+
B<<
C-
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction.
Not handling the zero case.
3fill in blank
hard

Fix the error in the code to correctly check if n is power of two.

DSA C
int isPowerOfTwo(unsigned int n) {
    if (n == 0) return 0;
    return (n & (n [1] 1)) == 0;
}
Drag options to blanks, or click blank then click option'
A-
B/
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication instead of subtraction.
Not checking for zero input.
4fill in blank
hard

Fill both blanks to create a function that returns 1 if n is power of two, else 0.

DSA C
int isPowerOfTwo(unsigned int n) {
    if (n == [1]) return 0;
    return (n & (n [2] 1)) == 0;
}
Drag options to blanks, or click blank then click option'
A0
B1
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if n equals 1 instead of 0.
Using addition instead of subtraction.
5fill in blank
hard

Fill all three blanks to complete the function that returns 1 if n is power of two, else 0.

DSA C
int isPowerOfTwo(unsigned int n) {
    if (n == [1]) return [2];
    return (n & (n [3] 1)) == 0;
}
Drag options to blanks, or click blank then click option'
A1
B0
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 when n is zero.
Using addition instead of subtraction.