Let there be a number 'x' be there.What we watn to achieve is to sqp the first 'n' bits of 'x' with the other bits.

For eg. if x is 11010110 and we want to swap first 3 bits ( from left) i.e. 110 with the remaining bits 10110, so that the result is 10110110

If we not closely, the "swapped" result is nothing but the sum of two numbers (left + right); of which the first number has the right part (10110) filled with 3 zeros on left, and the second number has the left part (110) on right with five zeroes. i.e
10110000 (right padded with zeros on left)
+00000110 (left padded with zeros on right)
-------------------
10110110 (result)

The first number is achieved by shifting 'x' 3 places to right and the second number is achieved by shifting 'x' (8-3)=5 places to left.So done!.

int swapbit(int x, int n) { //Swap first n bits of x with rest)

int result,left,right;
left=x<
right=x>>(8-n);
result=left+right;
return (result);
}

COMPACT CODE
int swapbit(int x, int n){
return( (x<>(8-n));