Bird
0
0
DSA Cprogramming~10 mins

Reverse Bits of a Number 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 initialize the result variable to zero.

DSA C
unsigned int reverseBits(unsigned int n) {
    unsigned int result = [1];
    for (int i = 0; i < 32; i++) {
        // loop body
    }
    return result;
}
Drag options to blanks, or click blank then click option'
An
B1
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result with n instead of 0.
Using negative values for unsigned int.
2fill in blank
medium

Complete the code to extract the least significant bit of n.

DSA C
unsigned int reverseBits(unsigned int n) {
    unsigned int result = 0;
    for (int i = 0; i < 32; i++) {
        unsigned int bit = n [1] 1;
        n >>= 1;
        // use bit
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A&
B<<
C^
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR or XOR instead of AND to extract bits.
Using shift operators incorrectly here.
3fill in blank
hard

Fix the error in the code to shift the result left before adding the extracted bit.

DSA C
unsigned int reverseBits(unsigned int n) {
    unsigned int result = 0;
    for (int i = 0; i < 32; i++) {
        unsigned int bit = n & 1;
        n >>= 1;
        result = (result [1] 1) | bit;
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A>>
B<<
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift instead of left shift.
Using addition instead of bitwise OR.
4fill in blank
hard

Fill both blanks to complete the loop that reverses bits of n.

DSA C
unsigned int reverseBits(unsigned int n) {
    unsigned int result = 0;
    for (int i = 0; i [1] 0; i [2] 32) {
        unsigned int bit = n & 1;
        n >>= 1;
        result = (result << 1) | bit;
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or <= incorrectly in the loop condition.
Starting i at 1 instead of 0.
5fill in blank
hard

Fill all three blanks to create a function that reverses bits of an unsigned int.

DSA C
unsigned int reverseBits(unsigned int n) {
    unsigned int result = [1];
    for (int i = 0; i [2] 0; i [3] 32) {
        unsigned int bit = n & 1;
        n >>= 1;
        result = (result << 1) | bit;
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A0
B=
C<
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to initialize result to zero.
Using wrong loop conditions or increments.