Bird
0
0
DSA Cprogramming~10 mins

Why Bit Manipulation and When It Beats Arithmetic in DSA C - Test Your Knowledge

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 even using bit manipulation.

DSA C
int isEven(int num) {
    return (num & [1]) == 0;
}
Drag options to blanks, or click blank then click option'
A1
B0
C2
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 in the bitwise AND which always returns 0.
Using 2 which checks the second bit, not the first.
2fill in blank
medium

Complete the code to multiply a number by 8 using bit manipulation.

DSA C
int multiplyByEight(int num) {
    return num [1] 3;
}
Drag options to blanks, or click blank then click option'
A+
B*
C>>
D<<
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift which divides the number.
Using multiplication operator instead of bit shift.
3fill in blank
hard

Fix the error in the code to divide a number by 4 using bit manipulation.

DSA C
int divideByFour(int num) {
    return num [1] 2;
}
Drag options to blanks, or click blank then click option'
A>>
B<<
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using left shift which multiplies instead of dividing.
Using addition or subtraction operators.
4fill in blank
hard

Fill both blanks to toggle the 3rd bit of a number.

DSA C
int toggleThirdBit(int num) {
    return num [1] [2];
}
Drag options to blanks, or click blank then click option'
A^
B&
C1 << 2
D1 << 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using bit 3 (index 3) instead of bit 2 for the 3rd bit.
Using AND which clears bits instead of toggling.
5fill in blank
hard

Fill all three blanks to set the 5th bit of a number and clear the 2nd bit.

DSA C
int setAndClearBits(int num) {
    num = num [1] [2];
    num = num [3] ~(1 << 1);
    return num;
}
Drag options to blanks, or click blank then click option'
A|
B&
C~
D1 << 4
Attempts:
3 left
💡 Hint
Common Mistakes
Using XOR to clear bits which toggles instead.
Using wrong bit positions for setting or clearing.