Yes, it is very possible. Just use a bitwise exclusive OR, or just an XOR operator for a number with a power of 2 n, where n is the digit you want to change. ^ is the XOR operator in C.
000000 (decimal 0) ^ 100000 (decimal 32 = 2 power 5 = 1 << 5) = 100000 1010 (decimal 10) XOR 0010 (decimal 2 = 2 power 1 = 1 << 1) = 1000
You can calculate 2 by power n by simply shifting the bits by 1 by n bits. Thus, 2 to 4 power can be obtained by shifting the bits in 1 to 4 places.
inputNum ^ (1 <n) will give what you need if switching is all you need.
Bitwise XOR "^" bit a bit ba ^ b (a XOR b) 0 0 0 0 1 1 1 0 1 1 1 0
However, remember that executing XOR on a bit that already has 1 will convert it to zero. because 1 ^ 1 = 0;
If you just want to convert 0 to 1 and save 1 if it already exists. You may need to use the bitwise operator Or.
Bitwise OR "|" bit a bit ba | b (a OR b) 0 0 0 0 1 1 1 0 1 1 1 1
Below is an example
11001110 | 10011000 = 11011110
Source: http://en.wikipedia.org/wiki/Bitwise_operations_in_C and http://en.wikipedia.org/wiki/Bitwise_operation
Buddha
source share