C Program to Replace Bits from Specified Position

This is a C Program to replace bits in integer from specified positions from another integer

Problem Description

This C Program counts total number of words in the sentence using command line argument.

Problem Solution

Take input from the user and counts total number of words in the sentence as shown in the program below.

Program/Source Code

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;
}
Program Explanation

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.

advertisement
advertisement
Runtime Test Cases

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.

Note: Join free Sanfoundry classes at Telegram or Youtube
$ 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.

advertisement

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

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.