All submissions

Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // Write your code here
    int mask = (1 << 5);

    int result = n & mask; 

    if (!result){
        n = n | (1 << 5); 
        
    }else {
        n = n & ~(1 << 5); 
    }
    return n; 
}

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

Solving Approach

First i made sure if the bit is 0 or 1. The int variable result is calculating that by using mask. 

If the bit is 0, we can toggle the fifth bit as n = n | (1 << 5). 

If the bit is 1, we can toggle the fifth bit to zero as n= n&~(1<< 5)

 

 

Loading...

Input

8

Expected Output

40