2. Bit Toggle

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // 5th bit not set
    if(!(n & (1<<5)))
        // set 5th bit
        n |= (1U << 5);
    // 5th bit is set
    else
        // clear 5th bit
        n &= ~(1U << 5);

    return n;
}

int main() {
    int n;
    scanf("%d", &n);
    printf("%d", toggleFifthBit(n));
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote