Bird
Raised Fist0
DSA Rustprogramming~10 mins

Check if Number is Even or Odd Using Bits in DSA Rust - Interactive Practice

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

Complete the code to check if a number is even using bitwise AND.

DSA Rust
fn is_even(num: i32) -> bool {
    num & [1] == 0
}

fn main() {
    let number = 4;
    println!("Is even? {}", is_even(number));
}
Drag options to blanks, or click blank then click option'
A1
B0
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 in bitwise AND.
Checking if num & 1 equals 1 to find even numbers.
2fill in blank
medium

Complete the code to print "Odd" if the number is odd using bitwise operation.

DSA Rust
fn main() {
    let num = 7;
    if num & [1] == 1 {
        println!("Odd");
    } else {
        println!("Even");
    }
}
Drag options to blanks, or click blank then click option'
A0
B1
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 in the condition.
Using equality check with 0 to find odd numbers.
3fill in blank
hard

Fix the error in the function that returns true if a number is odd using bitwise operation.

DSA Rust
fn is_odd(num: i32) -> bool {
    num & [1] == 1
}

fn main() {
    let number = 5;
    println!("Is odd? {}", is_odd(number));
}
Drag options to blanks, or click blank then click option'
A2
B0
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 in bitwise AND.
Checking for equality with 0 to find odd numbers.
4fill in blank
hard

Fill both blanks to create a function that returns "Even" or "Odd" based on bitwise check.

DSA Rust
fn even_or_odd(num: i32) -> &'static str {
    if num & [1] == [2] {
        "Even"
    } else {
        "Odd"
    }
}

fn main() {
    let number = 10;
    println!("{}", even_or_odd(number));
}
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the values in the condition.
Using 2 or -1 instead of 1 or 0.
5fill in blank
hard

Fill all three blanks to create a function that returns true if a number is odd and false if even using bitwise operation.

DSA Rust
fn is_odd(num: i32) -> bool {
    num [1] [2] == [3]
}

fn main() {
    let number = 9;
    println!("Is odd? {}", is_odd(number));
}
Drag options to blanks, or click blank then click option'
A&
B1
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators like | or ^.
Comparing with 0 instead of 1.

Practice

(1/5)
1. What does the expression num & 1 check when determining if a number is even or odd?
easy
A. It checks the least significant bit of the number
B. It checks if the number is greater than 1
C. It divides the number by 2
D. It adds 1 to the number

Solution

  1. Step 1: Understand bitwise AND operation

    The expression num & 1 performs a bitwise AND between the number and 1, which isolates the least significant bit (rightmost bit).
  2. Step 2: Interpret the least significant bit

    If the least significant bit is 1, the number is odd; if it is 0, the number is even.
  3. Final Answer:

    It checks the least significant bit of the number -> Option A
  4. Quick Check:

    Least significant bit = odd/even check [OK]
Hint: Check last bit with & 1 to find odd or even fast [OK]
Common Mistakes:
  • Thinking & 1 divides the number
  • Confusing bitwise AND with addition
  • Assuming it checks if number > 1
2. Which Rust code snippet correctly checks if a number n is odd using bitwise operations?
easy
A. if n ^ 1 == 1 { println!("Odd"); }
B. if n | 1 == 1 { println!("Odd"); }
C. if n & 0 == 1 { println!("Odd"); }
D. if n & 1 == 1 { println!("Odd"); }

Solution

  1. Step 1: Identify correct bitwise operator for odd check

    The bitwise AND operator & with 1 isolates the least significant bit, which indicates oddness.
  2. Step 2: Verify condition correctness

    Checking if n & 1 == 1 means the number is odd. Other operators like OR | or XOR ^ do not correctly check oddness.
  3. Final Answer:

    if n & 1 == 1 { println!("Odd"); } -> Option D
  4. Quick Check:

    Use & 1 == 1 to check odd [OK]
Hint: Use & 1 == 1 to check odd numbers in Rust [OK]
Common Mistakes:
  • Using OR or XOR instead of AND
  • Comparing with 0 instead of 1
  • Using bitwise operators incorrectly
3. What will be the output of this Rust code?
let num = 14;
if num & 1 == 0 {
    println!("Even");
} else {
    println!("Odd");
}
medium
A. Even
B. Odd
C. 14
D. Compilation error

Solution

  1. Step 1: Evaluate bitwise AND of 14 and 1

    Binary of 14 is 1110, and 1 is 0001. 1110 & 0001 = 0.
  2. Step 2: Check condition and print output

    Since num & 1 == 0 is true, the code prints "Even".
  3. Final Answer:

    Even -> Option A
  4. Quick Check:

    14 & 1 = 0 means Even [OK]
Hint: If num & 1 equals 0, number is even [OK]
Common Mistakes:
  • Assuming 14 is odd
  • Confusing bitwise AND with arithmetic AND
  • Expecting output to be the number itself
4. Identify the error in this Rust code snippet that checks if a number is odd:
let n = 7;
if n & 1 = 1 {
    println!("Odd");
} else {
    println!("Even");
}
medium
A. Using & instead of && in if condition
B. Missing semicolon after println!
C. Using single = instead of == in if condition
D. Variable n is not declared

Solution

  1. Step 1: Check the if condition syntax

    The condition uses single equals = which is assignment, not comparison.
  2. Step 2: Correct the comparison operator

    It should use double equals == to compare n & 1 with 1.
  3. Final Answer:

    Using single = instead of == in if condition -> Option C
  4. Quick Check:

    Use == for comparison, not = [OK]
Hint: Use == for comparison, not = assignment [OK]
Common Mistakes:
  • Confusing assignment (=) with equality (==)
  • Thinking & is logical AND instead of bitwise
  • Ignoring compiler error messages
5. Given a list of integers nums = [2, 3, 4, 5, 6], which Rust code correctly uses bitwise operations to filter only odd numbers and print them?
hard
A. for num in nums.iter() { if num & 0 == 1 { println!("{}", num); } }
B. for &num in &nums { if num & 1 == 1 { println!("{}", num); } }
C. for num in nums { if num | 1 == 1 { println!("{}", num); } }
D. for num in nums { if num ^ 1 == 1 { println!("{}", num); } }

Solution

  1. Step 1: Understand iteration and bitwise odd check

    Iterate over references to numbers and use num & 1 == 1 to check odd numbers.
  2. Step 2: Verify correct syntax and logic

    for &num in &nums { if num & 1 == 1 { println!("{}", num); } } correctly iterates with for &num in &nums and checks oddness with bitwise AND. Other options misuse operators or iteration.
  3. Final Answer:

    for &num in &nums { if num & 1 == 1 { println!("{}", num); } } -> Option B
  4. Quick Check:

    Use & 1 == 1 inside loop over &nums [OK]
Hint: Iterate references and use & 1 == 1 to print odd numbers [OK]
Common Mistakes:
  • Using wrong bitwise operators like | or ^
  • Iterating without references causing ownership errors
  • Checking with & 0 instead of & 1
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