This is a C Program to swap the ith and jth bits for a 32-bit integer.

This C Program swaps the ith and jth bits for a 32-bit integer.

Take input from the user and swaps the ith and jth bits as shown in the program below.

Here is source code of the C Program to swap the ith and jth bits for a 32-bit integer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C Program to Swap the ith and jth Bits for a 32-Bit Integer */ #include <stdio.h> int swap(int ,int); int number, pos1, pos2; int main() { int result, shift_pos1, shift_pos2; printf("\nEnter Number"); scanf("%d", &number); printf("\nEnter bit positions to swap"); scanf("%d %d", &pos1, &pos2); shift_pos1 = number >> pos1; shift_pos2 = number >> pos2; result = swap(shift_pos1&1, shift_pos2&1); printf("%d\n", result); } int swap(int pos1_val, int pos2_val) { int temp1, temp2; long int base, base1; /* * If the pos1_val value is 1 then only pos2_val th bit is set to 1 by using << and + operators */ if (pos1_val == 1) { base1 = 1 << pos2; number = number + base1; } /* *If the pos2_val value is 1 then only pos2_val th bit is set to 1 by using << and + operators */ if (pos2_val == 1) { base1 = 1 << pos2; number = number + base1; } /* *If the pos1_val value is 0 then only pos2_val th bit is set to 0 using <<, ^ and & operators */ if (pos1_val == 0) { base = 0XFFFFFFFF; base1 = 1 << pos2; temp1 = base ^ base1; number = number & temp1; } /* *If the pos2_val value is 0 then only pos1_val th bit is set to 0 using <<, ^ and & operators */ if (pos2_val == 0) { base = 0XFFFFFFFF; base1 = 1 << pos1; temp2 = base ^ base1; number = number & temp2; } return number; }

In this C Program, we are reading the number using ‘number’ variable and also the bit positions using ‘pos1’ and ‘pos2’ variables respectively. The ‘shift_pos1’ and ‘shift_pos2’ variables are used to move the left operands value to right by the number of bits specified by the right operands.

The swap function is used to swap the ith and jth bits for a 32-bit integer. Compute the Binary AND operation to shift_pos1 and shift_pos2 value by copying a bit to the result if it exists in both operands and pass the value as an argument. If condition statement is used to check the value of ‘pos1_val’ variable is equal to 1. If the condition is true then assign the ‘pos2_val’ th bit to 1 by using << and + operators.

Another if condition statement is used to check the value of ‘pos2_val’ variable is equal to 1. If the condition is true then assign pos2_val th bit to 1 by using << and + operators.

Another if condition statement is used to check the value of ‘pos1_val’ variable is equal to 0. If the condition is true then assign the pos2_val th bit 0 using <<, ^ and & operators.

Another if condition statement is used to check the value of ‘pos2_val’ variable is equal to 0. If the condition is true then assign ‘pos1_val’ th bit is set to 0 using <<, ^ and & operators and return the value of ‘number’ variable.

$ gcc bit31.c $ a.out Enter Number101 Enter bit positions to swap2 4 113

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms