Your function fail because this line
n = n & (0 >> i);
is just erasing
n
.
To clear 1 bit the way you try, you need to use the not operation:
n = n & ( ~ (1 >> i));
But with the xor operation, your code can be simplified to:
for(i=0; i< sizeof(int) * 8; i++)
{
if(n & (a >> i))
n = n & (0 >> i);
else
n = n | (1 >> i);
n = n ^ (1 >> i);
}
Bitwise operations can change all bits at same time, the not can further simplify the code to 1 line:
n = ~ n;
Bitwise operations in C - Wikipedia[
^]