Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to multiply a number by 2 using bit manipulation.
DSA Rust
let x = 5; let result = x [1] 1; println!("{}", result);
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift (>>) instead of left shift (<<).
Using addition (+) instead of bit shift.
✗ Incorrect
Shifting bits to the left by 1 (<< 1) multiplies the number by 2.
2fill in blank
mediumComplete the code to check if a number is even using bit manipulation.
DSA Rust
let x = 10; let is_even = (x [1] 1) == 0; println!("{}", is_even);
Attempts:
3 left
💡 Hint
Common Mistakes
Using bitwise OR (|) instead of AND (&).
Using left shift (<<) which changes the number.
✗ Incorrect
Using bitwise AND (&) with 1 checks the least significant bit to determine evenness.
3fill in blank
hardFix the error in the code to divide a number by 4 using bit manipulation.
DSA Rust
let x = 20; let result = x [1] 2; println!("{}", result);
Attempts:
3 left
💡 Hint
Common Mistakes
Using left shift (<<) which multiplies instead of divides.
Using addition or subtraction instead of bit shifts.
✗ Incorrect
Right shifting (>> 2) divides the number by 4 by moving bits two places to the right.
4fill in blank
hardFill both blanks to toggle the 3rd bit of a number using bit manipulation.
DSA Rust
let x = 8; let mask = 1 [1] 2; let result = x [2] mask; println!("{}", result);
Attempts:
3 left
💡 Hint
Common Mistakes
Using AND (&) instead of XOR (^) to toggle bits.
Shifting bits in the wrong direction.
✗ Incorrect
Shift 1 left by 2 to create a mask for the 3rd bit, then XOR (^) toggles that bit.
5fill in blank
hardFill all three blanks to set the 5th bit of a number using bit manipulation.
DSA Rust
let x = 10; let mask = 1 [1] 4; let result = x [2] mask; println!("{}", result); // Check if 5th bit is set let is_set = (result [3] mask) != 0; println!("{}", is_set);
Attempts:
3 left
💡 Hint
Common Mistakes
Using XOR (^) to set bits which toggles instead.
Using wrong shift amount for the mask.
Using OR (|) to check bits instead of AND (&).
✗ Incorrect
Shift 1 left by 4 to create mask for 5th bit, use OR (|) to set the bit, and AND (&) to check if bit is set.
