/* * Program to swap bit in given 32 bit Integer number */ #include<stdio.h> int main() { int a; int b1, b2; printf("Enter numbers \n"); scanf("%d", &a); printf("Enter bit numbers to be swapped\n"); scanf("%d%d", &b1, &b2); if(b1 >=31 || b2>=31){ printf("Invalid bit positions(valid are 0 - 31)\n"); return 0; } if((a&(1<<b1)) && !(a&(1<<b2))){ /* bit number b1 is set but b2 is not * to swap just unset b1 and set b2 */ a=a & ~(1<<b1); a=a | (1 << b2); } else if(!(a&(1<<b1)) && (a&(1<<b2))){ /* bit b1 is not set(zero) and bit b2 is set * so to swap set b1 and clear b2 */ a=a | (1<<b1); a=a & ~(1<<b2); } else { /* given both bits are set/clear in number so nothing to be done*/ } printf("Number after bit swap is %d\n", a); return 0;
C and Linux with Examples!!!