📖 Scenario: Imagine you have a simple control panel with 8 switches represented by bits in a number. Each bit can be ON (1) or OFF (0). You want to learn how to turn a specific switch ON, OFF, or flip its state using bit operations.
🎯 Goal: You will write a Java program that can set (turn ON), clear (turn OFF), or toggle (flip) a specific bit in an 8-bit number. This helps you control individual switches easily.
📋 What You'll Learn
Create an integer variable called number with the value 0b00001100 (decimal 12).
Create an integer variable called bitPosition with the value 2.
Write code to set the bit at bitPosition in number.
Write code to clear the bit at bitPosition in number.
Write code to toggle the bit at bitPosition in number.
Print the results after each operation in binary format.
💡 Why This Matters
🌍 Real World
Controlling individual bits is useful in hardware programming, setting flags, and optimizing memory usage.
💼 Career
Understanding bit manipulation is important for embedded systems, game development, and performance-critical applications.
Progress0 / 4 steps
1
Create the initial number
Create an integer variable called number and set it to 0b00001100 (which is decimal 12).
DSA Java
Hint
Use int number = 0b00001100; to create the variable.
2
Create the bit position variable
Create an integer variable called bitPosition and set it to 2.
DSA Java
Hint
Use int bitPosition = 2; to create the variable.
3
Set, clear, and toggle the bit
Write code to set the bit at bitPosition in number and store it in setNumber. Then clear the bit at bitPosition in number and store it in clearNumber. Finally, toggle the bit at bitPosition in number and store it in toggleNumber.
DSA Java
Hint
Use bitwise OR | with 1 << bitPosition to set the bit.
Use bitwise AND & with the negation ~(1 << bitPosition) to clear the bit.
Use bitwise XOR ^ with 1 << bitPosition to toggle the bit.
4
Print the results in binary
Print the values of setNumber, clearNumber, and toggleNumber in binary format using Integer.toBinaryString(). Print each on a new line with labels: "Set: ", "Clear: ", and "Toggle: ".
DSA Java
Hint
Use System.out.println("Set: " + Integer.toBinaryString(setNumber)); and similar lines for clear and toggle.
Practice
(1/5)
1. What does the operation num | (1 << k) do to the integer num in terms of its bits?
easy
A. Toggles the k-th bit of num
B. Clears the k-th bit of num to 0
C. Sets the k-th bit of num to 1
D. Checks if the k-th bit of num is set
Solution
Step 1: Understand the bit shift operation
1 << k creates a number with only the k-th bit set to 1 and all others 0.
Step 2: Apply bitwise OR with num
Using num | (1 << k) sets the k-th bit of num to 1 without changing other bits.
Final Answer:
Sets the k-th bit of num to 1 -> Option C
Quick Check:
Set bit = OR with 1 shifted [OK]
Hint: Use OR with 1 shifted to set a bit [OK]
Common Mistakes:
Confusing OR with AND operation
Thinking it clears or toggles the bit
Misunderstanding bit positions starting at 0
2. Which of the following Java statements correctly clears (sets to 0) the 3rd bit (0-based) of an integer variable num?
easy
A. num = num & (1 << 3);
B. num = num & ~(1 << 3);
C. num = num ^ (1 << 3);
D. num = num | (1 << 3);
Solution
Step 1: Understand clearing a bit
To clear a bit, we AND with a mask that has 0 at that bit and 1s elsewhere.
Step 2: Create mask and apply
~(1 << 3) creates a mask with all 1s except 0 at bit 3. ANDing num with this clears bit 3.
Final Answer:
num = num & ~(1 << 3); -> Option B
Quick Check:
Clear bit = AND with NOT mask [OK]
Hint: Clear bit by AND with NOT of shifted 1 [OK]
Common Mistakes:
Using OR instead of AND to clear bits
Using XOR which toggles bits
Using AND with shifted 1 which keeps only that bit
3. What is the output of the following Java code?
int num = 10; // binary 1010
int k = 1;
num = num ^ (1 << k);
System.out.println(num);
medium
A. 10
B. 14
C. 12
D. 8
Solution
Step 1: Understand initial value and bit positions
num = 10 in binary is 1010. Bit positions (0-based) from right: bit0=0, bit1=1, bit2=0, bit3=1.
Step 2: Apply XOR to toggle bit 1
1 << 1 is 0010. XOR toggles bit 1: 1010 ^ 0010 = 1000 (binary) which is 8 decimal.
Final Answer:
8 -> Option D
Quick Check:
XOR toggles bit 1: 10 -> 8 [OK]
Hint: XOR toggles the bit at position k [OK]
Common Mistakes:
Confusing XOR with OR or AND
Misreading bit positions starting at 1 instead of 0
Incorrect binary to decimal conversion
4. The following Java code is intended to toggle the 2nd bit of num. What is the error?
int num = 5; // binary 0101
int k = 2;
num = num & (1 << k);
System.out.println(num);
medium
A. It clears all bits except the 2nd bit instead of toggling
B. It toggles the wrong bit
C. It causes a syntax error
D. It sets the 2nd bit to 1 always
Solution
Step 1: Analyze the operation used
The code uses AND with 1 << k, which keeps only the k-th bit and clears others.
Step 2: Understand toggle operation
Toggle requires XOR, not AND. AND here does not toggle but masks all bits except bit 2.
Final Answer:
It clears all bits except the 2nd bit instead of toggling -> Option A
Quick Check:
Toggle = XOR, not AND mask [OK]
Hint: Toggle bits using XOR, not AND [OK]
Common Mistakes:
Using AND instead of XOR to toggle bits
Confusing bit positions
Expecting output without understanding bitwise operations
5. Given an integer num, write a Java expression to toggle the 0th bit only if it is currently set (1), otherwise leave num unchanged. Which expression achieves this?
hard
A. num = (num & (1 << 0)) != 0 ? num ^ (1 << 0) : num;
B. num = num & ~(1 << 0);
C. num = num ^ (1 << 0);
D. num = num | (1 << 0);
Solution
Step 1: Check if 0th bit is set
Use (num & (1 << 0)) != 0 to test if bit 0 is 1.
Step 2: Toggle bit 0 only if set
If bit 0 is set, toggle it with XOR; else keep num unchanged.
Final Answer:
num = (num & (1 << 0)) != 0 ? num ^ (1 << 0) : num; -> Option A
Quick Check:
Conditional toggle only if bit set [OK]
Hint: Use conditional check before toggling bit [OK]
Common Mistakes:
Toggling bit unconditionally
Using OR which always sets bit
Clearing bit without condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.