0
0
Rustprogramming~10 mins

Bitwise operators in Rust - Interactive Code Practice

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

Complete the code to perform a bitwise AND between two numbers.

Rust
let result = 5 [1] 3;
println!("{}", result);
Drag options to blanks, or click blank then click option'
A|
B&
C^
D<<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using | instead of & will perform bitwise OR, which is different.
Using ^ will perform XOR, which is not the same as AND.
2fill in blank
medium

Complete the code to perform a bitwise OR between two numbers.

Rust
let result = 5 [1] 3;
println!("{}", result);
Drag options to blanks, or click blank then click option'
A^
B&
C|
D>>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using & instead of | will perform AND, which is different.
Using ^ will perform XOR, which is not the same as OR.
3fill in blank
hard

Fix the error in the code to perform a bitwise XOR between two numbers.

Rust
let result = 5 [1] 3;
println!("{}", result);
Drag options to blanks, or click blank then click option'
A^
B|
C<<
D&
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using & or | instead of ^ will give different results.
Using shift operators like << or >> is incorrect here.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps numbers to their bitwise left shift by 2, only for numbers greater than 2.

Rust
let shifted = std::collections::HashMap::from_iter((1..6).filter(|&x| x [1] 2).map(|x| (x, x [2] 2)));
println!("{:?}", shifted);
Drag options to blanks, or click blank then click option'
A>
B<
C<<
D>>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using < instead of > in the filter.
Using >> instead of << for left shift.
5fill in blank
hard

Fill all three blanks to create a HashMap that maps the bitwise NOT of numbers to their bitwise AND with 3, only for numbers less than 5.

Rust
let result = std::collections::HashMap::from_iter((0..10).filter(|&x| x [1] 5).map(|x| (!x, x [2] [3])));
println!("{:?}", result);
Drag options to blanks, or click blank then click option'
A<
B&
C3
D|
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using > instead of < in the filter.
Using | instead of & for bitwise AND.
Not using 3 as the mask value.