How to swap ith and jth Bits for a 32-Bit Integer?
We can also reduce the no of instruction through this
int bit_swap(int num, int i, int j) { return( ( ((n >> i) & 1) == ((n >> j) & 1) ) ? n : (n ^ ( (1<<i) | (1 << j) ) )); }
int bit_swap(int num, int i,int j) { int m = (num >> i) & 1; /* TO KNOW THE PARTICULAR DESTINATION BIT */ int n = (num >> j) & 1; /* TO KNOW THE PARTICULAR SOURCE BIT */ if (m != n) { /* IF BOTH ARE NOT EQUAL */ num = num ^ (1 << i); num = num ^ (1 << j); } return num; }
For example, 00000001 11001100 becomes 11001100 00000001;
unsigned short swap_bytes(unsigned short x) {
}
Write a program to reverse the bits of an unsigned integer ??