Discover how a few simple commands can control many switches perfectly every time!
Why Set Clear Toggle a Specific Bit in DSA Rust?
Imagine you have a row of light switches, each controlling a different lamp. You want to turn on, turn off, or flip just one lamp without touching the others.
Doing this by checking each lamp one by one is slow and confusing.
Manually checking and changing each lamp's state takes a lot of time and can cause mistakes, like turning off the wrong lamp or missing one.
It's hard to keep track of which lamp is on or off when you have many lamps.
Using bit operations lets you control each lamp (bit) directly and quickly.
You can set (turn on), clear (turn off), or toggle (flip) a specific bit with simple commands, without affecting others.
let mut lamps = [false, false, false, false]; lamps[2] = true; // turn on lamp 3 lamps[2] = false; // turn off lamp 3 lamps[2] = !lamps[2]; // flip lamp 3
let mut lamps: u8 = 0b0000_0000; lamps |= 1 << 2; // set bit 2 lamps &= !(1 << 2); // clear bit 2 lamps ^= 1 << 2; // toggle bit 2
You can control many on/off states efficiently and safely with just a few commands.
Microcontrollers use bit operations to control hardware pins, turning devices on or off without affecting others.
Manual control of individual states is slow and error-prone.
Bit operations let you set, clear, or toggle specific bits quickly.
This makes managing many on/off states easy and efficient.
Practice
num | (1 << pos) do to the bit at position pos in num?Solution
Step 1: Understand the bitwise OR with shifted 1
The expression1 << poscreates a number with only the bit at positionposset to 1, all others 0.Step 2: Apply OR operation to set the bit
Usingnum | (1 << pos)sets the bit atposto 1 regardless of its previous value, leaving other bits unchanged.Final Answer:
Sets the bit at position pos to 1 -> Option AQuick Check:
OR with 1 at pos sets bit to 1 [OK]
- Confusing OR with XOR for toggling
- Thinking it clears the bit instead of setting
- Assuming it checks the bit instead of modifying
pos in num?Solution
Step 1: Create mask with 0 at pos and 1 elsewhere
The expression!(1 << pos)creates a mask with all bits 1 except bitposwhich is 0.Step 2: AND operation clears bit at pos
Usingnum & !(1 << pos)clears the bit atposto 0, keeping other bits unchanged.Final Answer:
num & !(1 << pos) -> Option BQuick Check:
AND with inverted mask clears bit [OK]
- Using OR instead of AND to clear bits
- Forgetting to invert the mask
- Using XOR which toggles instead of clears
let mut num = 0b1010; // binary 10
let pos = 1;
num ^= 1 << pos;
println!("{:04b}", num);Solution
Step 1: Understand initial value and bit position
Initialnumis binary 1010 (decimal 10). Positionposis 1 (counting from 0 at right).Step 2: Toggle bit at position 1 using XOR
1 << posis 0010. XOR toggles bit 1: 1010 XOR 0010 = 1000 (decimal 8).Final Answer:
1000 -> Option AQuick Check:
XOR toggles bit 1: 1010 -> 1000 [OK]
- Confusing XOR with OR or AND
- Miscounting bit positions
- Printing decimal instead of binary
pos in num:
let mut num = 0b1001;
let pos = 2;
num = num | (1 << pos);
num = num ^ (1 << pos);
println!("{:04b}", num);Solution
Step 1: Analyze the first operation (OR)
num | (1 << pos)sets bit 2 to 1. Bit 2 was 0 in 1001, so num becomes 1101.Step 2: Analyze the second operation (XOR)
num ^ (1 << pos)toggles bit 2 from 1 back to 0, resulting in 1001.Step 3: Check final output
After both operations, bit 2 was set then toggled back, so no net change occurs.Final Answer:
The bit is toggled twice, so no change occurs -> Option CQuick Check:
Setting then toggling same bit cancels out [OK]
- Assuming OR then XOR always toggles bit
- Ignoring bit position indexing
- Expecting compile error where none exists
num = 0b11001100, write the result after toggling bits at positions 2, 3, and 7 in sequence using Rust bitwise operations. What is the final binary value?Solution
Step 1: Initial value and bit positions
Initialnumis 11001100 (bits 7 to 0). Positions to toggle: 2, 3, 7.Step 2: Toggle bit 2
Bit 2 is 1 (counting from 0 rightmost). Toggle flips it to 0: 11001100 -> 11001000.Step 3: Toggle bit 3
Bit 3 is 1. Toggle flips it to 0: 11001000 -> 11000000.Step 4: Toggle bit 7
Bit 7 is 1. Toggle flips it to 0: 11000000 -> 01000000.Step 5: Final binary value
Final value is 01000000 in binary (bits 7,3,2 toggled to 0, others unchanged).Final Answer:
01000000 -> Option DQuick Check:
Toggle bits 2,3,7 flips those bits [OK]
- Miscounting bit positions from right
- Forgetting to toggle bits in sequence
- Confusing toggle with set or clear
Ready to prove it?
Pass the test and this pattern moves unknown
