0
0
Rustprogramming~5 mins

Bitwise operators in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the bitwise AND operator (&) do in Rust?
It compares each bit of two numbers and returns a new number where each bit is 1 only if both bits in the original numbers were 1; otherwise, the bit is 0.
Click to reveal answer
beginner
Explain the bitwise OR operator (|) in Rust.
It compares each bit of two numbers and returns a new number where each bit is 1 if at least one of the bits in the original numbers was 1; otherwise, the bit is 0.
Click to reveal answer
beginner
What is the purpose of the bitwise XOR operator (^) in Rust?
It compares each bit of two numbers and returns a new number where each bit is 1 if the bits are different (one is 1 and the other is 0); if the bits are the same, the bit is 0.
Click to reveal answer
beginner
How does the bitwise NOT operator (~) work in Rust?
It flips every bit of a number: all 1 bits become 0, and all 0 bits become 1. Note: In Rust, the bitwise NOT operator is ~, not !.
Click to reveal answer
intermediate
What do the left shift (<<) and right shift (>>) operators do in Rust?
Left shift (<<) moves all bits in a number to the left by a specified number of places, adding zeros on the right. Right shift (>>) moves bits to the right, discarding bits on the right and filling in on the left depending on the type.
Click to reveal answer
What is the result of 5 & 3 in Rust? (5 = 0101, 3 = 0011 in binary)
A0
B1
C6
D7
Which operator flips all bits of a number in Rust?
A~
B|
C&
D^
What does the expression 4 << 1 evaluate to in Rust?
A8
B2
C5
D1
Which bitwise operator returns 1 only if bits are different?
A&
B|
C~
D^
What is the result of 7 | 2 in Rust? (7 = 0111, 2 = 0010 in binary)
A5
B3
C7
D2
Describe how the bitwise AND, OR, and XOR operators work with simple examples.
Think about comparing bits like switches being on or off.
You got /4 concepts.
    Explain what happens when you use left shift (<<) and right shift (>>) operators on a number.
    Imagine sliding beads on a string to the left or right.
    You got /3 concepts.