You can determine whether or not a particular bit is turned on by using the & operator, ie:
Code:
if (value & 0x01)
if (value & 0x02)
if (value & 0x04)
if (value & pow(2,0))
if (value & pow(2,1))
if (value & pow(2,2))
To ensure a bit is turned on, do this:
To ensure it is off, do this:
Note that those last two code pieces will ensure the bit state no matter what it was orginally, so they're safe to use without first checking the state.
I suggest a function signature like this:
Code:
int SwapBits(int value, int index1,int index2);
Good luck. Have a shot, then post any problems you have.