Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Check if Number is Even or Odd Using Bits in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bitwise Even-Odd Master
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 TypeScript code using bitwise AND to check even or odd?
Consider the following TypeScript code that checks if a number is even or odd using bitwise operations. What will it print?
DSA Typescript
function checkEvenOdd(num: number): string {
  return (num & 1) === 0 ? "Even" : "Odd";
}

console.log(checkEvenOdd(10));
console.log(checkEvenOdd(7));
A"Odd\nOdd"
B"Odd\nEven"
C"Even\nEven"
D"Even\nOdd"
Attempts:
2 left
💡 Hint
The bitwise AND with 1 checks the least significant bit of the number.
❓ Predict Output
intermediate
2:00remaining
What is the output of this code that uses bitwise XOR to toggle even/odd check?
This TypeScript function uses bitwise XOR to check if a number is even or odd. What will it print?
DSA Typescript
function isOdd(num: number): boolean {
  return (num ^ 1) === num - 1;
}

console.log(isOdd(4));
console.log(isOdd(5));
Afalse\ntrue
Btrue\nfalse
Cfalse\nfalse
Dtrue\ntrue
Attempts:
2 left
💡 Hint
XOR with 1 flips the last bit, so for even numbers it adds 1, for odd numbers it subtracts 1.
🔧 Debug
advanced
2:00remaining
What error does this TypeScript code produce when checking even or odd using bits?
This code attempts to check if a number is even or odd using bitwise AND but has a mistake. What error will it raise?
DSA Typescript
function check(num: number): string {
  if (num & 1 === 0) {
    return "Even";
  } else {
    return "Odd";
  }
}

console.log(check(3));
AReferenceError
BTypeError
CSyntaxError
DNo error, prints Odd
Attempts:
2 left
💡 Hint
Check the use of '=' inside the if condition.
🧠 Conceptual
advanced
2:00remaining
Which statement about using bitwise AND to check even or odd is correct?
Select the correct statement about using the expression (num & 1) to check if a number is even or odd.
AIf (num & 1) equals 0, the number is even.
BIf (num & 1) equals 1, the number is even.
CBitwise AND with 1 returns the number divided by 2.
DBitwise AND with 1 always returns 0 for odd numbers.
Attempts:
2 left
💡 Hint
Think about the least significant bit in binary for even and odd numbers.
🚀 Application
expert
2:00remaining
What is the output of this TypeScript code that filters even numbers using bitwise AND?
Given the following code that filters even numbers from an array using bitwise AND, what is the printed output?
DSA Typescript
const numbers = [1, 2, 3, 4, 5, 6];
const evens = numbers.filter(n => (n & 1) === 0);
console.log(evens);
A[]
B[2, 4, 6]
C[1, 2, 3, 4, 5, 6]
D[1, 3, 5]
Attempts:
2 left
💡 Hint
Filter keeps numbers where the last bit is 0.

Practice

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

Solution

  1. Step 1: Understand bitwise AND with 1

    The expression num & 1 isolates the least significant bit (rightmost bit) of the number.
  2. Step 2: Interpret the least significant bit

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

    It checks if the least significant bit of num is 1, indicating odd number. -> Option B
  4. Quick Check:

    num & 1 = 1 means odd [OK]
Hint: Check last bit with & 1; 1 means odd, 0 means even [OK]
Common Mistakes:
  • Thinking & 1 checks divisibility by 2
  • Confusing bitwise AND with logical AND
  • Assuming it checks if number is negative
2. Which of the following TypeScript code snippets correctly checks if a number n is even using bitwise operators?
easy
A. if ((n & 1) === 0) { console.log('Even'); } else { console.log('Odd'); }
B. if ((n | 1) === 0) { console.log('Even'); } else { console.log('Odd'); }
C. if ((n & 0) === 1) { console.log('Even'); } else { console.log('Odd'); }
D. if ((n ^ 1) === 0) { console.log('Even'); } else { console.log('Odd'); }

Solution

  1. Step 1: Understand bitwise AND with 1

    The expression n & 1 extracts the least significant bit of n.
  2. Step 2: Check if least significant bit is zero

    If (n & 1) === 0, the number is even; otherwise, it is odd.
  3. Final Answer:

    if ((n & 1) === 0) { console.log('Even'); } else { console.log('Odd'); } -> Option A
  4. Quick Check:

    Use n & 1 === 0 for even check [OK]
Hint: Use (n & 1) === 0 to check even numbers [OK]
Common Mistakes:
  • Using bitwise OR instead of AND
  • Comparing with wrong values like 0 or 1 incorrectly
  • Using XOR without understanding its effect
3. What will be the output of the following TypeScript code?
const numbers = [2, 3, 4, 5];
numbers.forEach(num => {
  if (num & 1) {
    console.log(num + ' is Odd');
  } else {
    console.log(num + ' is Even');
  }
});
medium
A. 2 is Odd 3 is Odd 4 is Odd 5 is Odd
B. 2 is Even 3 is Even 4 is Even 5 is Even
C. 2 is Odd 3 is Even 4 is Odd 5 is Even
D. 2 is Even 3 is Odd 4 is Even 5 is Odd

Solution

  1. Step 1: Evaluate bitwise AND for each number

    For each number, num & 1 checks the last bit: 2 & 1 = 0 (even), 3 & 1 = 1 (odd), 4 & 1 = 0 (even), 5 & 1 = 1 (odd).
  2. Step 2: Print corresponding output

    Numbers with num & 1 === 1 print 'Odd', others print 'Even'.
  3. Final Answer:

    2 is Even 3 is Odd 4 is Even 5 is Odd -> Option D
  4. Quick Check:

    Bitwise check matches even/odd output [OK]
Hint: Check each number's last bit; 1 means odd, 0 means even [OK]
Common Mistakes:
  • Assuming all numbers are odd
  • Confusing bitwise AND with logical AND
  • Printing wrong labels for even/odd
4. Identify the error in this TypeScript code that tries to check if a number x is odd using bits:
function isOdd(x: number): boolean {
  return x & 1 === 1;
}
medium
A. The bitwise AND operator has lower precedence than ===, causing wrong evaluation.
B. The function should use bitwise OR instead of AND.
C. The function should return a string, not boolean.
D. There is no error; the code works correctly.

Solution

  1. Step 1: Check operator precedence in expression

    Bitwise AND (&) has lower precedence than strict equality (===), so x & 1 === 1 is evaluated as x & (1 === 1).
  2. Step 2: Understand the evaluation result

    1 === 1 is true, which converts to 1, so expression becomes x & 1. This returns a number, not a boolean, causing unexpected behavior.
  3. Step 3: Fix the expression with parentheses

    Use parentheses: (x & 1) === 1 to ensure correct evaluation order.
  4. Final Answer:

    The bitwise AND operator has lower precedence than ===, causing wrong evaluation. -> Option A
  5. Quick Check:

    Use parentheses: (x & 1) === 1 [OK]
Hint: Use parentheses around bitwise operation before comparison [OK]
Common Mistakes:
  • Ignoring operator precedence
  • Using bitwise OR instead of AND
  • Returning wrong data type
5. You want to filter an array of numbers to keep only the even ones using bitwise operations in TypeScript. Which code snippet correctly achieves this?
const nums = [10, 15, 22, 33, 40];
const evens = nums.filter( /* your code here */ );
console.log(evens);
hard
A. nums.filter(num => (num & 1) === 1)
B. nums.filter(num => (num | 1) === 0)
C. nums.filter(num => (num & 1) === 0)
D. nums.filter(num => (num ^ 1) === 0)

Solution

  1. Step 1: Understand filtering condition for even numbers

    Even numbers have their least significant bit as 0, so (num & 1) === 0 checks for even numbers.
  2. Step 2: Apply filter with correct condition

    Using nums.filter(num => (num & 1) === 0) returns only even numbers from the array.
  3. Step 3: Verify output

    For [10, 15, 22, 33, 40], the filtered array is [10, 22, 40].
  4. Final Answer:

    nums.filter(num => (num & 1) === 0) -> Option C
  5. Quick Check:

    Filter even numbers with (num & 1) === 0 [OK]
Hint: Filter with (num & 1) === 0 to get even numbers [OK]
Common Mistakes:
  • Using bitwise OR or XOR incorrectly
  • Filtering for odd numbers instead of even
  • Missing parentheses causing wrong evaluation
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