Bird
Raised Fist0
DSA Javaprogramming~10 mins

Set Clear Toggle a Specific Bit in DSA Java - Execution Trace

Choose your learning style10 modes available
Concept Flow - Set Clear Toggle a Specific Bit
Start with number n
↓
Choose bit position p
↓
Select operation: Set / Clear / Toggle
↓
Create mask = 1 << p
↓
If Set: n = n | mask
↓
If Clear: n = n & ~mask
↓
If Toggle: n = n ^ mask
↓
Result: n with bit p changed
↓
End
This flow shows how to change a specific bit in a number by creating a mask and applying bitwise operations to set, clear, or toggle that bit.
Execution Sample
DSA Java
int n = 13; // binary 1101
int p = 1;    // bit position to change
// Set bit p
n = n | (1 << p);
System.out.println(n);
This code sets the bit at position 1 of number 13 (binary 1101), changing it to 15 (binary 1111).
Execution Table
StepOperationNumber (decimal)Number (binary)Mask (binary)Bit PositionResult (decimal)Result (binary)
1Start with n131101-1--
2Create mask--00101--
3Set bit: n = n | mask13110100101151111
4Clear bit: n = n & ~mask15111100101131101
5Toggle bit: n = n ^ mask13110100101151111
6Toggle bit again15111100101131101
7End------
💡 Operations stop after toggling bit twice, returning to original number.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
n13 (1101)13 (1101)15 (1111)13 (1101)15 (1111)13 (1101)13 (1101)
mask-2 (0010)2 (0010)2 (0010)2 (0010)2 (0010)2 (0010)
p1111111
Key Moments - 3 Insights
Why do we use '1 << p' to create the mask?
Because shifting 1 left by p places creates a binary number with only the p-th bit set to 1, which is used to target that specific bit in the number. See Step 2 in execution_table.
Why does clearing a bit use '& ~mask' instead of just '& mask'?
Because '~mask' flips the mask bits, making all bits 1 except the target bit 0, so '& ~mask' clears that bit while keeping others unchanged. See Step 4 in execution_table.
How does toggling a bit with '^ mask' work?
XOR with mask flips the target bit: if it was 0, it becomes 1; if 1, it becomes 0. This is shown in Steps 5 and 6 where toggling flips the bit back and forth.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what is the binary result after setting bit 1?
A1101
B1111
C1011
D1001
💡 Hint
Check the 'Result (binary)' column at Step 3 in execution_table.
At which step does the number return to its original value after toggling bit 1 twice?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look at the 'Number (decimal)' column in execution_table after toggling twice.
If we change bit position p to 2, what would be the mask in binary at Step 2?
A0001
B0010
C0100
D1000
💡 Hint
Mask is created by shifting 1 left by p positions, see Step 2 mask for p=1.
Concept Snapshot
Set, Clear, Toggle a bit:
- Use mask = 1 << p to target bit p
- Set bit: n = n | mask (turn bit ON)
- Clear bit: n = n & ~mask (turn bit OFF)
- Toggle bit: n = n ^ mask (flip bit)
- Bit positions start at 0 from right (LSB)
- Useful for efficient bit manipulation
Full Transcript
This concept teaches how to change a specific bit in a number using bitwise operations. We start with a number n and a bit position p. We create a mask by shifting 1 left by p positions. To set the bit, we use OR with the mask. To clear the bit, we use AND with the negation of the mask. To toggle the bit, we use XOR with the mask. The execution table shows step-by-step how the number changes in decimal and binary form after each operation. The variable tracker follows the values of n, mask, and p through the steps. Key moments clarify why we use shifting to create the mask, why clearing uses AND with negated mask, and how toggling flips the bit. The visual quiz tests understanding of the binary results and mask creation. The snapshot summarizes the key rules and syntax for quick reference.

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

  1. 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.
  2. Step 2: Apply bitwise OR with num

    Using num | (1 << k) sets the k-th bit of num to 1 without changing other bits.
  3. Final Answer:

    Sets the k-th bit of num to 1 -> Option C
  4. 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

  1. Step 1: Understand clearing a bit

    To clear a bit, we AND with a mask that has 0 at that bit and 1s elsewhere.
  2. 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.
  3. Final Answer:

    num = num & ~(1 << 3); -> Option B
  4. 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

  1. 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.
  2. Step 2: Apply XOR to toggle bit 1

    1 << 1 is 0010. XOR toggles bit 1: 1010 ^ 0010 = 1000 (binary) which is 8 decimal.
  3. Final Answer:

    8 -> Option D
  4. 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

  1. Step 1: Analyze the operation used

    The code uses AND with 1 << k, which keeps only the k-th bit and clears others.
  2. Step 2: Understand toggle operation

    Toggle requires XOR, not AND. AND here does not toggle but masks all bits except bit 2.
  3. Final Answer:

    It clears all bits except the 2nd bit instead of toggling -> Option A
  4. 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

  1. Step 1: Check if 0th bit is set

    Use (num & (1 << 0)) != 0 to test if bit 0 is 1.
  2. Step 2: Toggle bit 0 only if set

    If bit 0 is set, toggle it with XOR; else keep num unchanged.
  3. Final Answer:

    num = (num & (1 << 0)) != 0 ? num ^ (1 << 0) : num; -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard