Bird
Raised Fist0
DSA Rustprogramming~20 mins

Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using bitwise AND and OR?
Consider the following Rust code snippet. What will be printed?
DSA Rust
fn main() {
    let a: u8 = 0b10101010;
    let b: u8 = 0b11001100;
    let c = a & b;
    let d = a | b;
    println!("{:08b} {:08b}", c, d);
}
A"10101010 11001100"
B"10001000 11111110"
C"10001000 11101110"
D"00110010 11101110"
Attempts:
2 left
💡 Hint
Remember bitwise AND (&) keeps bits set only if both bits are 1; OR (|) sets bits if either bit is 1.
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using XOR and NOT?
Look at this Rust code. What will it print?
DSA Rust
fn main() {
    let x: u8 = 0b11110000;
    let y = !x;
    let z = x ^ y;
    println!("{:08b} {:08b}", y, z);
}
A"00001111 11111111"
B"11110000 00001111"
C"11110000 11110000"
D"00001111 00000000"
Attempts:
2 left
💡 Hint
NOT (~) flips all bits; XOR (^) between a number and its NOT results in all bits set.
❓ Predict Output
advanced
2:00remaining
What is the output after left and right shifting in Rust?
Given this Rust code, what will be printed?
DSA Rust
fn main() {
    let n: u8 = 0b00001111;
    let left = n << 2;
    let right = n >> 1;
    println!("{:08b} {:08b}", left, right);
}
A"00111100 00001111"
B"00111100 00000111"
C"00001111 00000111"
D"00001111 00011110"
Attempts:
2 left
💡 Hint
Left shift (<<) moves bits left, adding zeros on right; right shift (>>) moves bits right, adding zeros on left.
🧠 Conceptual
advanced
1:30remaining
Which operation flips only the bits that are set in one operand but not the other?
In bit manipulation, which operator flips bits where exactly one of the two bits is 1?
ANOT (~)
BOR (|)
CAND (&)
DXOR (^)
Attempts:
2 left
💡 Hint
Think about which operator results in 1 only when bits differ.
🔧 Debug
expert
2:00remaining
What error occurs when shifting bits beyond the size of the integer in Rust?
Consider this Rust code snippet: fn main() { let x: u8 = 1; let y = x << 8; println!("{}", y); } What happens when you run this code?
AThe code causes a panic at runtime due to overflow
BThe code compiles and prints 1
CThe code compiles and prints 0
DThe code fails to compile with a syntax error
Attempts:
2 left
💡 Hint
Shifting by a number equal or larger than the bit size of the type causes runtime panic in Rust.

Practice

(1/5)
1. What is the result of the bitwise AND operation between 0b1101 and 0b1011?
easy
A. 0b1001
B. 0b1111
C. 0b0101
D. 0b0011

Solution

  1. Step 1: Understand bitwise AND operation

    Bitwise AND compares each bit of two numbers and returns 1 only if both bits are 1.
  2. Step 2: Apply AND to each bit of 0b1101 and 0b1011

    Bits: 1&1=1, 1&0=0, 0&1=0, 1&1=1, so result is 0b1001.
  3. Final Answer:

    0b1001 -> Option A
  4. Quick Check:

    1101 & 1011 = 1001 [OK]
Hint: AND keeps bits set only if both are 1 [OK]
Common Mistakes:
  • Confusing AND with OR operation
  • Mixing bit positions when calculating
  • Forgetting binary format in answer
2. Which of the following is the correct Rust syntax to perform a bitwise XOR between variables a and b?
easy
A. let c = a & b;
B. let c = a && b;
C. let c = a ^ b;
D. let c = a | b;

Solution

  1. Step 1: Recall Rust bitwise XOR operator

    In Rust, the caret symbol (^) is used for bitwise XOR.
  2. Step 2: Check each option's operator

    The options use: & (bitwise AND), && (logical AND), | (bitwise OR), ^ (bitwise XOR). Only ^ is correct.
  3. Final Answer:

    let c = a ^ b; -> Option C
  4. Quick Check:

    Bitwise XOR in Rust uses ^ [OK]
Hint: XOR uses ^ symbol in Rust [OK]
Common Mistakes:
  • Using && instead of ^ for XOR
  • Confusing bitwise OR (|) with XOR
  • Using logical operators instead of bitwise
3. What is the output of the following Rust code?
let x: u8 = 0b0011_1100;
let y: u8 = 0b0000_1101;
let z = x | y;
println!("{:08b}", z);
medium
A. 00111101
B. 00110001
C. 00001101
D. 00111100

Solution

  1. Step 1: Understand bitwise OR operation

    Bitwise OR returns 1 if at least one bit is 1 in either number.
  2. Step 2: Apply OR to each bit of x and y

    x = 00111100, y = 00001101; OR bits: 0|0=0,0|0=0,1|0=1,1|0=1,1|1=1,1|1=1,0|0=0,0|1=1; result = 00111101.
  3. Final Answer:

    00111101 -> Option A
  4. Quick Check:

    00111100 | 00001101 = 00111101 [OK]
Hint: OR sets bit if either bit is 1 [OK]
Common Mistakes:
  • Mixing OR with AND operation
  • Misreading binary digits
  • Ignoring leading zeros in output
4. Identify the error in this Rust code snippet that attempts to left shift a variable:
let a: u8 = 0b0001_0001;
let b = a << 8
println!("{:08b}", b);
medium
A. Incorrect use of << operator, should be >>
B. Shift amount exceeds variable bit size causing overflow
C. Cannot print binary format with {:08b}
D. Missing semicolon after variable declaration

Solution

  1. Step 1: Check for syntax errors

    The declaration 'let b = a << 8' is missing a semicolon (;).
  2. Step 2: Verify other parts

    << is correct for left shift; Rust masks large shift amounts without panic/overflow error; {:08b} works for binary output.
  3. Final Answer:

    Missing semicolon after variable declaration -> Option D
  4. Quick Check:

    Missing ; after let causes compile error [OK]
Hint: Rust statements must end with semicolon [OK]
Common Mistakes:
  • Omitting semicolons after let statements
  • Confusing left shift with right shift
  • Assuming shifts panic on large amounts
5. Given a 16-bit unsigned integer num, which Rust expression correctly clears (sets to 0) the 5th bit (counting from 0 at the right) without affecting other bits?
hard
A. num | (1 << 5)
B. num & !(1 << 5)
C. num ^ (1 << 5)
D. num << 5

Solution

  1. Step 1: Understand clearing a bit

    To clear a bit, use AND with a mask that has 0 at target bit and 1 elsewhere.
  2. Step 2: Create mask for 5th bit

    1 << 5 creates a mask with 1 at 5th bit; negating it with ! flips bits, making 0 at 5th bit.
  3. Step 3: Apply mask with AND

    num & !(1 << 5) clears 5th bit, keeps others unchanged.
  4. Final Answer:

    num & !(1 << 5) -> Option B
  5. Quick Check:

    Clear bit 5 using AND with inverted mask [OK]
Hint: Clear bit with AND and inverted mask [OK]
Common Mistakes:
  • Using OR instead of AND to clear bits
  • Using XOR which toggles bits
  • Shifting num instead of mask
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard