logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

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

Posted on May 27, 2013 by Manish
This C Program replace bits in integer x from bit position a to b from another integer y.

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.

  1. /*
  2.  * C Program to Replace Bits in Integer x from Bit Position a to b from another Integer y
  3.  */
  4. #include <stdio.h>
  5.  
  6. void changebits(int, int, int, int);
  7.  
  8. int main()
  9. {
  10.     int num1, num2, pos1, pos2;
  11.  
  12.     printf("**Replacing the bits in integer x from bit position a to b from another integer y**\n");
  13.     printf("read number 1\n");
  14.     scanf("%x", &num1);
  15.     printf("Read number 2:\n");
  16.     scanf("%x", &num2);
  17.     printf("Read LSB postion:\n");
  18.     scanf("%d", &pos1);
  19.     printf("MSB should always be greater than LSB\n");
  20.     printf("Read MSB position:\n");
  21.     scanf("%d", &pos2);
  22.     changebits(num1, num2, pos1, pos2);
  23.     return 0;
  24. }
  25.  
  26. /*Function to swap bits in given positions*/
  27.  
  28. void changebits(int num1, int num2, int pos1, int pos2)
  29. {
  30.     int temp1, temp_1, buffer2, bit1 = 0, bit2 = 0, counter = 0, a = 1;
  31.  
  32.     temp1 = num1;
  33.     temp_1 = num1;
  34.     buffer2 = num2;
  35.     for (;pos1 <= pos2;pos1++)        
  36.     {
  37.         a = 1;
  38.         num1 = temp_1;
  39.         num2 = buffer2;
  40.         while (counter <= pos1)
  41.         {
  42.             if (counter  == pos1)        
  43.                 bit1 = (num1&1);    //placing the bit of position 1 in bit1
  44.             counter++;
  45.             num1>> = 1;
  46.         }
  47.         counter = 0;
  48.         while (counter <= pos1)
  49.         {
  50.             if (counter == pos1)
  51.                 bit2 = (num2&1);        //placing the bit of position 2 in bit2
  52.             counter++;
  53.             num2 >>= 1;
  54.         }
  55.         counter = 0;
  56.         if (bit1 == bit2);
  57.         else
  58.         {
  59.             while (counter++<pos1)
  60.                 a = a << 1;                
  61.             temp1 ^= a;    //placing the repplaced bit integer into temp1 variable
  62.         }
  63.         counter = 0;
  64.     }
  65.     printf("the number num1 after shifting the bits is 0x%x\n", temp1);
  66. }

$ 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 Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Demonstrate Circular Single Linked List
» Next Page - C Program to Check whether a Tree is a Binary Search Tree
« C Program to Demonstrate Circular Single Linked List
C Program to Check whether a Tree is a Binary Search Tree »

Deep Dive @ Sanfoundry:

  1. Linux Program to Append to the End of a File using File Descriptors
  2. Linux Program to Write to a File using File Descriptors
  3. Linux Program to Duplicate a File Descriptor with fcntl()
  4. C# Programming Examples on Strings
  5. C Programming Examples on Arrays
  6. Linux Program to Create a File using File Descriptors
  7. C Programming Examples on Strings
  8. Linux Program to Open and Close a File using File Descriptors
  9. Linux Program to Read from a File using File Descriptors
  10. C Programming Examples on Bitwise Operations
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry