Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // Isolate if fifth bit is 0 or 1
    int fifthBit = (n & (1 << 5)) >> 5;
    if (fifthBit == 1){
        // clear only the 5th bit
        n &= ~(1 << 5);
    }
    else
    {
        // set that bit
        n |= (1 << 5);
    }
    return n;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

8

Expected Output

40