Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Check if Number is Even or Odd Using Bits in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Check if Number is Even or Odd Using Bits
Start with number N
↓
Extract least significant bit (N & 1)
↓
Is bit 0?
Yes→Number is Even
No↓
Number is Odd
We check the last bit of the number using bitwise AND with 1. If it is 0, the number is even; if 1, it is odd.
Execution Sample
DSA Typescript
function isEven(n: number): boolean {
  return (n & 1) === 0;
}

console.log(isEven(4));
console.log(isEven(7));
This code uses bitwise AND to check if the last bit of n is 0 (even) or 1 (odd).
Execution Table
StepOperationNumber (n)Bitwise AND (n & 1)Result (Even/Odd)
1Check number 44 (binary 100)4 & 1 = 0Even
2Check number 77 (binary 111)7 & 1 = 1Odd
3Check number 00 (binary 0)0 & 1 = 0Even
4Check number 11 (binary 1)1 & 1 = 1Odd
5Check number 1010 (binary 1010)10 & 1 = 0Even
6Check number 1515 (binary 1111)15 & 1 = 1Odd
7End of checks--Stop
💡 All numbers checked; bitwise AND with 1 determines even or odd.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
n447011015-
n & 1-010101-
Result-EvenOddEvenOddEvenOdd-
Key Moments - 3 Insights
Why does checking (n & 1) tell us if a number is even or odd?
Because the least significant bit (rightmost bit) of an even number is always 0, and for odd numbers it is 1, as shown in execution_table rows 1 and 2.
What happens if the number is zero? Is it even or odd?
Zero has its least significant bit as 0 (row 3), so it is considered even.
Why do we use bitwise AND with 1 instead of other numbers?
Bitwise AND with 1 isolates only the least significant bit, which determines even or odd, as demonstrated in all rows of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of (n & 1) when n = 10?
A10
B1
C0
DUndefined
💡 Hint
Check row 5 under 'Bitwise AND (n & 1)' column.
At which step does the number checked become odd for the first time?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Result' column in execution_table rows 1 and 2.
If we change the code to check (n & 2) instead of (n & 1), what would happen?
AIt still correctly checks even or odd
BIt checks if the second least significant bit is set
CIt always returns true
DIt causes an error
💡 Hint
Bitwise AND with 2 checks the second bit, not the least significant bit, so it changes the meaning.
Concept Snapshot
Check if number is even or odd using bits:
- Use bitwise AND: (n & 1)
- If result is 0 → number is even
- If result is 1 → number is odd
- Fast and efficient way without division
- Works for all integers including zero
Full Transcript
To check if a number is even or odd using bits, we look at the least significant bit of the number. This is done by performing a bitwise AND operation between the number and 1. If the result is 0, the number is even; if it is 1, the number is odd. For example, 4 in binary is 100, and 4 & 1 equals 0, so 4 is even. Similarly, 7 in binary is 111, and 7 & 1 equals 1, so 7 is odd. This method is efficient and works for all integers including zero. The execution table shows step-by-step checks for different numbers, confirming the logic. Key points include understanding why the least significant bit determines parity and why zero is considered even. The visual quiz reinforces these concepts by asking about specific steps and hypothetical code changes.

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