#include <stdio.h>
int toggleFifthBit(int n) {
// Write your code here
int pos = 5; //5th bit
n ^= (1<<pos);
return n;
}
int main() {
int n;
scanf("%d", &n);
printf("%d", toggleFifthBit(n));
return 0;
}
Solving Approach
Task is:
The program should take an integer N as input.
It should toggle the 5th bit of N (i.e., flip the bit at position 5: if 0, make it 1; if 1, make it 0).
We know that to toggle 'nth' position of the register, reg ^= (1<< n )
similarly, in the given task, reg -> n, and given position as 5 (pos == 5).
Replacing these values in the above gives the solution.