All submissions

Code

#include <stdio.h>

int toggleFifthBit(int n) {
    // Write your code here
    if(n&(1<<5))
        return n&=~(1<<5);
    else 
        return n|=(1<<5);

}

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

Solving Approach

  • Take an integer n as input.
  • Check if the 5th bit of n is set using n & (1 << 5).
  • If the 5th bit is 1, clear it using n & ~(1 << 5).
  • If the 5th bit is 0, set it using n | (1 << 5).
  • Return the modified number.

 

 

Loading...

Input

8

Expected Output

40