#include <stdio.h>
int toggleFifthBit(int n) {
// Write your code here
n ^= (1<<5); //Use XOR to toggle a bit at a certain location.
return n;
}
int main() {
int n;
scanf("%d", &n);
printf("%d", toggleFifthBit(n));
return 0;
}
Solving Approach
Use XOR to toggle the bit. XOR truth table is as follows:
A B Output (A)
0 0 0
1 0 1
0 1 1 -> Notice these 2 last rows. Assume that B is actually the bit that you do XOR operation.
1 1 0 -> Thus whenever XOR is active (1) the A is toggled from its original state.