0% found this document useful (0 votes)
4 views

bitops

The document contains multiple C programs that demonstrate bit manipulation techniques, including setting, clearing, inverting bits, counting set bits, checking if a number is a power of two, and interchanging bits and nibbles. Each program includes a function to display the binary representation of a number. The programs are designed to illustrate various operations on binary numbers using bitwise operators.

Uploaded by

Ananth Samiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

bitops

The document contains multiple C programs that demonstrate bit manipulation techniques, including setting, clearing, inverting bits, counting set bits, checking if a number is a power of two, and interchanging bits and nibbles. Each program includes a function to display the binary representation of a number. The programs are designed to illustrate various operations on binary numbers using bitwise operators.

Uploaded by

Ananth Samiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

//PROGRAM TO SET A PARTICULAR BIT IN A NUMBER

#include<stdio.h>
#define SET_BIT(n,d) (n | (1<<d-1))
int show(int);

int main()
{
int num,d;

printf("Enter number & the position which u want to set\n");


scanf("%d%d",&num,&d);

show(num);
num=SET_BIT(num,d);

show(num);

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

//PROGRAM TO CLEAR A PARTICULAR BIT IN A NUMBER

#include<stdio.h>
#define CLR_BIT(n,d) (n & ~(1<<d-1))
int show(int);

int main()
{
int num,d;
printf("Enter number & the position which u want to set\n");
scanf("%d%d",&num,&d);

show(num);
num=CLR_BIT(num,d);

show(num);

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

//PROGRAM TO INVER A PARTICULAR BIT IN A NUMBER

#include<stdio.h>
#define INVERT_BIT(n,d) (n ^ (1<<d-1))
int show(int);

int main()
{
int num,d;

printf("Enter number & the position which u want to set\n");


scanf("%d%d",&num,&d);

show(num);
num=INVERT_BIT(num,d);

show(num);
}

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

//PROGRAM TO INPUT A NUMBER AND DISPLAY IT IN A BINARY FORMAT

#include<stdio.h>

int show(int);

int main()
{
int num;

printf("Enter number\n");
scanf("%d",&num);

show(num);

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

}
//PROGRAM TO COUNT NUMBER OF SET BITS IN A NUMBER

#include<stdio.h>

int show(int);
int count(int);
int main()
{
int num;

printf("Enter number\n");
scanf("%d",&num);

show(num);
count(num);
}

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");
}

int count(int n)
{
int c=0;

while(n)
{
if(n & 1)
c++;

n = n>>1;
}

printf("Set bits are %d\n",c);

//PROGRAM TO FIND A NUMBER IS POWER OF TWO OR NOT

#include<stdio.h>

int show(int);
int count(int);
int main()
{
int num;
printf("Enter number\n");
scanf("%d",&num);

show(num);
count(num);
}

int show(int n)
{
int i;
for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");
printf("\n");
}

int count(int n)
{
int c=0;
while(n)
{
if(n & 1)
c++;
n = n>>1;
}

printf("Set bits are %d\n",c);

if(c==1)
printf("Power of 2\n");
}
//PROGRAM TO INTERCHANGE EVEN AND ODD POSITION BITS IN A NUMBER

#include<stdio.h>

int show(int);

int main()
{
int num, odd, even;

printf("Enter number\n");
scanf("%d",&num);

show(num);

odd=num & 0xAAAA;


even=num & 0x5555;

odd=odd>>1;
even=even<<1;

num=odd | even;

show(num);

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

}
//PROGRAM TO INTERCHANGE EVEN AND ODD POSITION NIBBLES IN A NUMBER

#include<stdio.h>

int show(int);

int main()
{
int num, odd, even;

printf("Enter number\n");
scanf("%d",&num);

show(num);

odd=num & 0xF0F0;


even=num & 0x0F0F;

odd=odd>>4;
even=even<<4;

num=odd | even;

show(num);

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

}
//PROGRAM TO FIND POSITION OF A SET BIT IN A NUMBER

#include<stdio.h>
#include<math.h>
int show(int);

int main()
{
int num, pos;

printf("Enter number\n");
scanf("%d",&num);

pos=log2(num) + 1;
show(num);

printf("position of set bit is %d\n",pos);


}

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

}
//PROGRAM TO FIND NUMBER OF BITS REQUIRED TO ALTER FOR A-->B

#include<stdio.h>

int show(int);
int count(int);
int main()
{
int a,b, num;

printf("Enter number a and b\n");


scanf("%d%d",&a,&b);

show(a);
show(b);

num=a ^ b;
show(num);
count(num);
}

int show(int n)
{
int i;
for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");
printf("\n");
}

int count(int n)
{
int c=0;
while(n)
{
if(n & 1)
c++;
n=n>>1;
}
printf("Total bits to alter are %d\n",c);
}
//PROGRAM TO RESET FIRST SET BIT OF A NUMBER

#include<stdio.h>

int show(int);

int main()
{
int num;

printf("Enter number\n");
scanf("%d",&num);

show(num);

num=num & ~(~num + 1);

show(num);

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

}
//PROGRAM TO FIND POSITION OF FIRST SET BIT IN A NUMBER

#include<stdio.h>
#include<math.h>
int show(int);

int main()
{
int num,pos;

printf("Enter number\n");
scanf("%d",&num);

show(num);

num=num & (~num + 1);

pos=log2(num)+1;

printf("First set bit is at %d position\n",pos);

int show(int n)
{
int i;

for(i=1<<15;i>0;i=i/2)
(n & i) ? printf("1") : printf("0");

printf("\n");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy