#include <stdio.h>
void showBits(int n, int size) {
for (int i = 1 << size - 1; i > 0; i = i / 2)
(n & i) ? printf("1") : printf("0");
}
int swap2Bits(unsigned int num, unsigned int firstPos, unsigned int secondPos) {
unsigned int firstBit = (num >> firstPos) & 1;
unsigned int secondBit = (num >> secondPos) & 1;
unsigned int xorBit = (firstBit ^ secondBit);
xorBit = (xorBit << firstPos) | (xorBit << secondPos);
return num ^ xorBit;
}
int main()
{
int n = 27;
showBits(n, 8);
int result = swap2Bits(n, 1, 6);
printf("\n");
showBits(result, 8);
return 0;
}
/*
run:
00011011
01011001
*/