📖 Scenario: Imagine you are building a simple tool that tells if a number is even or odd. Instead of using normal division, you will use a fast method with bits, like checking if a light switch is on or off.
🎯 Goal: Build a small program that uses bit operations to check if a number is even or odd and prints the result.
📋 What You'll Learn
Create a variable called number with the value 29
Create a variable called bitMask with the value 1
Use a bitwise AND operation between number and bitMask to check if the number is even or odd
Print "Odd" if the number is odd, otherwise print "Even"
💡 Why This Matters
🌍 Real World
Bitwise operations are used in low-level programming, game development, and performance-critical applications to quickly check or change data.
💼 Career
Understanding bitwise operations helps in roles like embedded systems developer, systems programmer, and software engineer working on optimization.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 29.
DSA Javascript
Hint
Use const number = 29; to create the variable.
2
Create the bitMask variable
Create a variable called bitMask and set it to 1.
DSA Javascript
Hint
Use const bitMask = 1; to create the variable.
3
Check if number is even or odd using bitwise AND
Use an if statement to check if number & bitMask is equal to 1. If yes, the number is odd; otherwise, it is even.
DSA Javascript
Hint
Use if ((number & bitMask) === 1) to check odd, else even.
4
Print the result
Run the program and print "Odd" if the number is odd, otherwise print "Even".
DSA Javascript
Hint
Run the code to see the output Odd because 29 is odd.
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
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.
Step 2: Check the result of number & 1
If the result is 0, the number is even; if 1, it is odd.
Final Answer:
AND with 1 (number & 1) -> Option D
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
Step 1: Understand the bitwise AND check
Using (num & 1) extracts the last bit. If it equals 1, the number is odd.
Step 2: Match expression to odd check
Expression (num & 1) === 1 returns true only for odd numbers.
Final Answer:
(num & 1) === 1 -> Option C
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?