C program to implement bit flipping.

Write a C program to implement bit flipping.
C Program to setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged in the least number of lines.
Bit flipping or flip bit is the complement(~) of bits. i.e., 0 to 1 and vise versa. bit flipping is the bits manipulation or processing individual bits within a byte. Bit flipping is used in the very low-level programming and is often used in graphics and systems programming.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <math.h>
#include <limits.h>

unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y) {
x |= (y & ~(~0 << n)) << p;
size_t s = (int)(log(INT_MAX)/log(2)) + 1;
printf("nnnSuccess! Number after bit flipping: n", s);
int mask = pow(2.0, (int)s);
do {
((x & mask) == 0) ? printf("0") : printf("1");
((s%4)==0) ? printf(" ") : printf("");
x <<= 1;
} while (s--);
printf("n");
}


void main( ) {
unsigned retrn=1, begin=0, nbits=0, num=0;

printf("Enter the number to bit flip!n");
scanf("%d",&num);
printf("Enter the number of bits to be flip!n");
scanf("%d",&nbits);
printf("Enter the position to begin bit flip!n");
scanf("%d",&begin);
unsigned x = setbits(retrn, begin, nbits, num);

}
Read more Similar C Programs
C Basic
C Search Algorithms
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply