Bird
0
0
DSA Cprogramming

Set Clear Toggle a Specific Bit in DSA C

Choose your learning style9 modes available
Mental Model
We change one specific bit in a number to 1, 0, or flip it without affecting other bits.
Analogy: Imagine a row of light switches where each switch controls one light. Setting a bit is like turning a specific switch on, clearing is turning it off, and toggling is flipping its current state.
Number bits: 0 0 1 0 1 1 0 1
Positions:  7 6 5 4 3 2 1 0
Toggle bit 3:
Before:     0 0 1 0 1 1 0 1
After:      0 0 1 0 0 1 0 1
Dry Run Walkthrough
Input: number = 45 (binary 00101101), bit position = 3
Goal: Set bit 3 to 1, clear bit 3 to 0, and toggle bit 3
Step 1: Set bit 3 to 1 using OR with mask 1 << 3
00101101 (45) OR 00001000 (8) = 00101101 (45)
Why: Bit 3 is already 1, so setting it keeps the number same
Step 2: Clear bit 3 to 0 using AND with NOT mask ~(1 << 3)
00101101 (45) AND 11110111 (247) = 00100101 (37)
Why: Clearing bit 3 changes it from 1 to 0
Step 3: Toggle bit 3 using XOR with mask 1 << 3
00100101 (37) XOR 00001000 (8) = 00101101 (45)
Why: Toggling flips bit 3 back to 1
Result:
Final values: Set bit 3: 45 (00101101)
Clear bit 3: 37 (00100101)
Toggle bit 3: 45 (00101101)
Annotated Code
DSA C
#include <stdio.h>

// Set bit at position pos to 1
unsigned int setBit(unsigned int num, int pos) {
    return num | (1U << pos); // OR sets bit at pos
}

// Clear bit at position pos to 0
unsigned int clearBit(unsigned int num, int pos) {
    return num & ~(1U << pos); // AND with NOT clears bit at pos
}

// Toggle bit at position pos
unsigned int toggleBit(unsigned int num, int pos) {
    return num ^ (1U << pos); // XOR flips bit at pos
}

int main() {
    unsigned int number = 45; // 00101101
    int position = 3;

    unsigned int set_result = setBit(number, position);
    unsigned int clear_result = clearBit(number, position);
    unsigned int toggle_result = toggleBit(clear_result, position);

    printf("Set bit %d: %u (binary 00101101)\n", position, set_result);
    printf("Clear bit %d: %u (binary 00100101)\n", position, clear_result);
    printf("Toggle bit %d: %u (binary 00101101)\n", position, toggle_result);

    return 0;
}
return num | (1U << pos);
Set bit at position pos by OR with mask
return num & ~(1U << pos);
Clear bit at position pos by AND with inverted mask
return num ^ (1U << pos);
Toggle bit at position pos by XOR with mask
OutputSuccess
Set bit 3: 45 (binary 00101101) Clear bit 3: 37 (binary 00100101) Toggle bit 3: 45 (binary 00101101)
Complexity Analysis
Time: O(1) because bitwise operations take constant time regardless of input size
Space: O(1) because no extra memory is used beyond variables
vs Alternative: Using bitwise operations is much faster and simpler than converting to binary strings and manipulating characters
Edge Cases
bit position is 0 (least significant bit)
Operations correctly affect the rightmost bit
DSA C
return num | (1U << pos);
bit position is maximum for unsigned int (e.g., 31)
Operations correctly affect the highest bit without overflow
DSA C
return num & ~(1U << pos);
number is 0
Setting bit sets that bit to 1, clearing keeps 0, toggling flips bit
DSA C
return num ^ (1U << pos);
When to Use This Pattern
When you need to change a single bit in a number without affecting others, use bitwise set, clear, or toggle operations because they are fast and direct.
Common Mistakes
Mistake: Using left shift without unsigned suffix causing undefined behavior
Fix: Use 1U to ensure shifting unsigned integer
Mistake: Clearing bit using OR instead of AND with NOT mask
Fix: Use AND with inverted mask to clear bit
Mistake: Toggling bit using OR or AND instead of XOR
Fix: Use XOR to flip the bit
Summary
It changes one specific bit in a number to 1, 0, or flips it.
Use it when you want to manipulate individual bits efficiently.
The key is using bitwise OR to set, AND with NOT to clear, and XOR to toggle bits.