All submissions

First, reset or clear the state of the bit if already set [ &= ~ ]

Then, set the bit if already cleared [ |= ]

 

 

Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // Write your code here
    unsigned int pos = 1 << 5;
    
    // Check current state of the bit
    if (n & pos) 
    {
        // Clear the bit if set
        n &= ~pos;
    } 
    else 
    {
        // Set the bit if clear
        n |= pos;
    }
    return n;
}

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

Input

8

Expected Output

40