Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The least significant bit (LSB) of an even number is 0. Using num & 1 checks this bit.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift which divides the number.
Using multiplication operator instead of bit shift.
✗ Incorrect
Left shifting a number by 3 bits multiplies it by 23 = 8.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using left shift which multiplies instead of dividing.
Using addition or subtraction operators.
✗ Incorrect
Right shifting by 2 bits divides the number by 22 = 4.
4fill in blank
hardFill 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'
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.
✗ Incorrect
XOR (^) with 1 << 2 toggles the 3rd bit (bit index 2).
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using XOR to clear bits which toggles instead.
Using wrong bit positions for setting or clearing.
✗ Incorrect
Use OR (|) with 1 << 4 to set the 5th bit, then AND (&) with the negation (~) of 1 << 1 to clear the 2nd bit.
