This is a C Program to replace bits in integer from specified positions from another integer
This C Program counts total number of words in the sentence using command line argument.
Take input from the user and counts total number of words in the sentence as shown in the program below.
Here is source code of the C Program to Count the total number of words in the sentence using command line argument. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Replace Bits in Integer from Specified Positions from * Another Integer */ #include <stdio.h> int main () { int num1 = 0, num2 = 0, i = 0, j = 0, xor = 0, res = 0; printf ("Enter the first number: "); scanf ("%d", &num1); printf ("\nEnter the second number: "); scanf ("%d", &num2); printf ("Enter the i'th bit in num1 to replace with j'th bit in num2: "); scanf ("%d", &i); printf ("\nEnter the j'th bit in num2 to replace with i'th bit in num1: "); scanf ("%d", &j); printf ("\n"); if (num1 == num2 && i == j) { printf ("%d",num1); printf ("\n"); return 0; } xor = ((num1 >> i) ^ (num2 >> j)) & 1; res = num1 ^ (xor << i) ^ (xor ^ j); printf ("\nResult = %d\n", res); return 0; }
1. Take the input numbers from the user and store it in num1 & num2 variables. Prompt user to enter the position of ith bit in num1 which they want to replace jth bit in num2.
2. If loop is used to check whether both the numbers entered by the user are the same and also i == j. This gives us no effect, hence it return.
3. Fetch the jth bit from the num2 and ith bit from num1 by using xor = ((num1 >> i) ^ (num2 >> j)) & 1;
4. Now replace the ith bit in num1 with that of jth bit in num2 by using res = num1 ^ (xor << i) ^ (xor ^ j);
5. Print the result from res variable using print statement.
Test case 1 – Here, the first and second numbers are same.
$ gcc replace_bits.c -o replace $ ./replace Enter the first number: 10 Enter the second number: 10 Enter the i'th bit in num1 to replace with j'th bit in num2: 2 Enter the j'th bit in num2 to replace with i'th bit in num1: 1 Result = 14
Test case 2 – Here, the first and second numbers are single digit numbers.
$ gcc replace_bits.c -o replace $ ./replace Enter the first number: 2 Enter the second number: 9 Enter the i'th bit in num1 to replace with j'th bit in num2: 1 Enter the j'th bit in num2 to replace with i'th bit in num1: 2 Result = 3
Test case 3 – Here, the first and second numbers are double digit numbers.
$ gcc replace_bits.c -o replace $ ./replace Enter the first number: 99 Enter the second number: 100 Enter the i'th bit in num1 to replace with j'th bit in num2: 2 Enter the j'th bit in num2 to replace with i'th bit in num1: 5 Result = 99
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Apply for C Internship
- Check C Books