Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Check if Number is Even or Odd Using Bits
Start with number n
↓
Perform bitwise AND: n & 1
↓
Is result 0?
Yes→Number is Even
No↓
Number is Odd
The flow checks the last bit of the number using bitwise AND with 1. If the last bit is 0, the number is even; if 1, it is odd.
Execution Sample
DSA Javascript
function isEven(n) {
  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
StepOperationInput Number (n)Bitwise AND (n & 1)ResultConclusion
1Check 4400 === 0Even
2Check 7711 !== 0Odd
3End---All numbers checked
💡 All input numbers processed; bitwise AND determines even or odd by last bit.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
n-47-
n & 1-01-
Result (isEven)-truefalse-
Key Moments - 2 Insights
Why does checking n & 1 tell us if a number is even or odd?
Because the last bit of a binary number is 0 for even numbers and 1 for odd numbers, as shown in execution_table rows 1 and 2.
What if the number is negative? Does this method still work?
Yes, because bitwise AND with 1 only checks the last bit, which indicates even or odd regardless of sign, similar to the positive examples in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of (n & 1) when n is 7?
A1
B7
C0
DUndefined
💡 Hint
Check execution_table row 2 under 'Bitwise AND (n & 1)' column.
At which step does the code conclude the number is even?
AStep 3
BStep 2
CStep 1
DNone
💡 Hint
Look at execution_table row 1 under 'Conclusion' column.
If we check number 10, what would (n & 1) be?
A1
B0
C10
DCannot determine
💡 Hint
Recall that even numbers have last bit 0, as shown in variable_tracker and execution_table.
Concept Snapshot
Check if number is even or odd using bitwise AND:
- Use (n & 1) to check last bit
- If result is 0, number is even
- If result is 1, number is odd
- Works for positive and negative integers
- Fast and efficient bitwise operation
Full Transcript
This concept shows how to check if a number is even or odd using bits. We use the bitwise AND operator with 1 to check the last bit of the number. If the last bit is 0, the number is even; if it is 1, the number is odd. The code example uses this logic to return true for even numbers and false for odd numbers. The execution table traces checking numbers 4 and 7, showing the bitwise AND results and conclusions. The variable tracker shows how variables change after each step. Key moments clarify why this works and that it applies to negative numbers too. The quiz tests understanding of the bitwise AND results and conclusions. This method is a quick and simple way to determine even or odd status using bits.

Practice

(1/5)
1. Which bitwise operation can be used to check if a number is even or odd?
easy
A. Left shift by 1 (number << 1)
B. OR with 1 (number | 1)
C. XOR with 1 (number ^ 1)
D. AND with 1 (number & 1)

Solution

  1. Step 1: Understand bitwise AND with 1

    The least significant bit (rightmost bit) of a number determines if it is even or odd. ANDing with 1 extracts this bit.
  2. Step 2: Check the result of number & 1

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

    AND with 1 (number & 1) -> Option D
  4. Quick Check:

    Least significant bit check = AND with 1 [OK]
Hint: Check last bit with AND 1 to find even or odd [OK]
Common Mistakes:
  • Using OR instead of AND
  • Using XOR which flips bits
  • Using left shift which multiplies number
2. Which of the following JavaScript expressions correctly checks if num is odd using bitwise operators?
easy
A. (num & 1) === 0
B. (num | 1) === 1
C. (num & 1) === 1
D. (num ^ 1) === 0

Solution

  1. Step 1: Understand the bitwise AND check

    Using (num & 1) extracts the last bit. If it equals 1, the number is odd.
  2. Step 2: Match expression to odd check

    Expression (num & 1) === 1 returns true only for odd numbers.
  3. Final Answer:

    (num & 1) === 1 -> Option C
  4. Quick Check:

    Odd check = (num & 1) === 1 [OK]
Hint: Use (num & 1) === 1 to check odd numbers [OK]
Common Mistakes:
  • Checking for 0 instead of 1 for odd
  • Using OR or XOR instead of AND
  • Confusing equality check with assignment
3. What will be the output of the following JavaScript code?
function isEven(num) {
  return (num & 1) === 0 ? 'Even' : 'Odd';
}
console.log(isEven(10));
console.log(isEven(7));
medium
A. "Even" followed by "Odd"
B. "Odd" followed by "Even"
C. "Even" followed by "Even"
D. "Odd" followed by "Odd"

Solution

  1. Step 1: Evaluate isEven(10)

    10 in binary ends with 0, so (10 & 1) === 0 is true, returns 'Even'.
  2. Step 2: Evaluate isEven(7)

    7 in binary ends with 1, so (7 & 1) === 0 is false, returns 'Odd'.
  3. Final Answer:

    "Even" followed by "Odd" -> Option A
  4. Quick Check:

    10 is even, 7 is odd [OK]
Hint: Check last bit: 0 means even, 1 means odd [OK]
Common Mistakes:
  • Mixing up output order
  • Assuming bitwise AND returns boolean directly
  • Confusing bitwise AND with logical AND
4. The following code is intended to check if a number is odd. What is the error?
function checkOdd(num) {
  if (num & 1 === 1) {
    return 'Odd';
  } else {
    return 'Even';
  }
}
medium
A. Missing parentheses around (num & 1)
B. Using bitwise OR instead of AND
C. Incorrect return values
D. Function missing return type

Solution

  1. Step 1: Analyze operator precedence

    Bitwise AND (&) has lower precedence than ===, so expression evaluates as num & (1 === 1).
  2. Step 2: Fix expression with parentheses

    Correct expression is (num & 1) === 1 to check last bit properly.
  3. Final Answer:

    Missing parentheses around (num & 1) -> Option A
  4. Quick Check:

    Use parentheses to ensure correct bitwise check [OK]
Hint: Use parentheses: (num & 1) === 1 to avoid precedence errors [OK]
Common Mistakes:
  • Ignoring operator precedence
  • Using bitwise OR instead of AND
  • Assuming bitwise AND returns boolean
5. You want to filter an array of numbers to keep only even numbers using bitwise operations. Which code snippet correctly does this?
const numbers = [3, 4, 7, 8, 10];
const evens = numbers.filter(num => ???);
console.log(evens);
hard
A. num & 1 === 1
B. (num & 1) === 0
C. num | 1 === 0
D. (num ^ 1) === 0

Solution

  1. Step 1: Understand filtering even numbers

    Even numbers have last bit 0, so (num & 1) === 0 is true for evens.
  2. Step 2: Apply filter condition

    Using numbers.filter(num => (num & 1) === 0) keeps only even numbers.
  3. Final Answer:

    (num & 1) === 0 -> Option B
  4. Quick Check:

    Filter evens with (num & 1) === 0 [OK]
Hint: Filter evens by checking (num & 1) === 0 [OK]
Common Mistakes:
  • Using odd check instead of even
  • Using OR or XOR instead of AND
  • Missing parentheses causing wrong precedence
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