2. Bit Toggle

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // Standard practice assumes 0-indexing. 
    // The 5th bit is index 5 (which represents 2^5).
    unsigned int mask = (1 << 5); 
    
    // Toggle the bit using XOR (^)
    n = n ^ mask; 
    
    return n;
}

int main() {
    int n;

    scanf("%d", &n);
    
    printf("%d\n", toggleFifthBit(n));
    
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote