C Program to Replace Bits in Integer x from Bit Position a to b from Another Integer y in C

This is a C Program to replace bits in integer x from bit position a to b from another integer y.

Problem Description

This C Program replace bits in integer x from bit position a to b from another integer y.

Problem Solution

Take input from the user and performs bits operation as shown in the program below.

Program/Source Code

Here is source code of the C Program to replace bits in integer x from bit position a to b from another integer y. 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 x from Bit Position a to b from another Integer y
 */
#include <stdio.h>
 
void changebits(int, int, int, int);
 
int main()
{
    int num1, num2, pos1, pos2;
 
    printf("**Replacing the bits in integer x from bit position a to b from another integer y**\n");
    printf("read number 1\n");
    scanf("%x", &num1);
    printf("Read number 2:\n");
    scanf("%x", &num2);
    printf("Read LSB postion:\n");
    scanf("%d", &pos1);
    printf("MSB should always be greater than LSB\n");
    printf("Read MSB position:\n");
    scanf("%d", &pos2);
    changebits(num1, num2, pos1, pos2);
    return 0;
}
 
/*Function to swap bits in given positions*/
 
void changebits(int num1, int num2, int pos1, int pos2)
{
    int temp1, temp_1, buffer2, bit1 = 0, bit2 = 0, counter = 0, a = 1;
 
    temp1 = num1;
    temp_1 = num1;
    buffer2 = num2;
    for (;pos1 <= pos2;pos1++)        
    {
        a = 1;
        num1 = temp_1;
        num2 = buffer2;
        while (counter <= pos1)
        {
            if (counter  == pos1)        
                bit1 = (num1&1);    //placing the bit of position 1 in bit1
            counter++;
            num1>> = 1;
        }
        counter = 0;
        while (counter <= pos1)
        {
            if (counter == pos1)
                bit2 = (num2&1);        //placing the bit of position 2 in bit2
            counter++;
            num2 >>= 1;
        }
        counter = 0;
        if (bit1 == bit2);
        else
        {
            while (counter++<pos1)
                a = a << 1;                
            temp1 ^= a;    //placing the repplaced bit integer into temp1 variable
        }
        counter = 0;
    }
    printf("the number num1 after shifting the bits is 0x%x\n", temp1);
}
Program Explanation

This C program, we are reading two integer numbers using ‘num1’ and ‘num2’ variables respectively, and the Least Significant Bit (LSB) and Most Significant Bit (MSB) using ‘pos1’ and ‘pos2’ variables respectively. The changebits() function is used to swap bits in given positions. Interchange the value of ‘num1’ variable to ‘temp1’ variable and the value of ‘num2’ variable to ‘buffer2’ variable.

advertisement
advertisement

For loop is used for replacing the bits, assign the value of ‘temp1’ variable to ‘num1’ and the value of ‘buffer2’ variable to ‘num2’. While is used for placing the bit of position 1 in bit1. Check the value of ‘counter’ variable is less than or equal to the value of ‘pos1’ variable.

If the condition is true then execute the while loop. If condition statement is used to check that the value of ‘counter’ variable is equal to the value of ‘pos1’ variable. Place the bit of position 1 in bit1 and increment the value of ‘counter’ variable. Using binary right shift operator the 1 value is moved right by the number of bits specified by the value of ‘num1’ variable and assign to ‘num1’ variable.

Another while loop is used for placing the bit of position 2 in bit2, Using binary right shift operator the 1 value is moved right by the number of bits specified by the value of ‘num2’ variable and assign to ‘num2’ variable. Then if-else condition statement is used for placing the replaced bit integer ‘a’ into ‘temp1’ variable. Print the replaced bits in integer x from bit position ‘a’ to ‘b’ from another Integer y.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
$ cc bit13.c
$ a.out
**Replacing the bits in integer x from bit position a to b from another integer y**
read number 1
0x11223344
Read number 2:
0x55667788
Read LSB postion:
12
MSB should always be greater than LSB
Read MSB position:
19
the number num1 after shifting the bits is 0x11267344

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

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

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.