#include <stdio.h>
int toggleFifthBit(int n) {
// toggle 5th bit in the n
return n ^= (1<<5);
}
int main() {
int n;
scanf("%d", &n);
printf("%d", toggleFifthBit(n));
return 0;
}
Solving Approach
In C, bit positions are 0-indexed.
1 << 0 → least significant bit (LSB)
1 << 5 → the 6th bit if you count from 1.
If you really want to toggle the 5th bit counting from 0, it 1 << 5 is correct. If you want to toggle the 5th bit counting from 1, you should use 1 << 4.