# 13.2 – Functions on Bit-wise operators part-2

by on January 8, 2018

3) Develop a function called offbit() to OFF a bit at a position

Logic:

Refer session  6.12 – Bitwise operators – AND, OR and XOR biwise operators to OFF a bit at a position

Program:

```#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int offbit(int,int);
void showbits(int);
int main()
{
int n,p;
printf("Enter any integer:");
scanf("%d",&n);
showbits(n);
printf("\nWhich least significant bit to OFF:");
scanf("%d",&p);
n=offbit(n,p);
showbits(n);
return 0;
}
int offbit(int x,int pos)
{
nofb=sizeof(x)*8;
/* position must not exceeds to the number of bits */
if(pos-1>nofb)
{
printf("Position overflows..");
exit(0);
}
/* anding with 1's compliment of mask operand */
return x;
}
void showbits(int x)
{
int i,bit,nofb;
/* sizeof bits differs platform to platform*/
nofb=sizeof(x)*8;
for(i=nofb-1;i>=0;i--)
{
bit=(x>>i)&1;
printf("%d",bit);
if(i%8==0&&i!=0)
printf("-");
}
}
```

Execution using gcc:
Enter any integer:425
00000000-00000000-00000001-10101001
Which least significant bit to OFF:8
00000000-00000000-00000001-00101001

Execution using Turbo C:
Enter any integer:425
00000001-10101001
Which least significant bit to OFF:8
00000001-00101001

4) Develop a function called togglebit() to toggle a bit at a position

Logic:

Refer session  6.12 – Bitwise operators – AND, OR and XOR biwise operators to OFF a bit at a position

Program:

```#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int togglebit(int,int);
void showbits(int);
int main()
{
int n,p;
printf("Enter any integer:");
scanf("%d",&n);
showbits(n);
printf("\nWhich least significant bit to toggle:");
scanf("%d",&p);
n=togglebit(n,p);
showbits(n);
return 0;
}
int togglebit(int x,int pos)
{
nofb=sizeof(x)*8;
/* position must not exceeds to the number of bits */
if(pos-1>nofb)
{
printf("Position overflows..");
exit(0);
}
/* xoring with mask operand */
return x;
}
void showbits(int x)
{
int i,bit,nofb;
/* sizeof bits differs platform to platform*/
nofb=sizeof(x)*8;
for(i=nofb-1;i>=0;i--)
{
bit=(x>>i)&1;
printf("%d",bit);
if(i%8==0&&i!=0)
printf("-");
}
}
```

Execution 1:
Enter any integer:425
00000000-00000000-00000001-10101001
Which least significant bit to toggle:9
00000000-00000000-00000000-10101001

Execution 2:
Enter any integer:425
00000000-00000000-00000001-10101001
Which least significant bit to toggle:7
00000000-00000000-00000001-11101001

5) Develop a function called findbit() to find the bit at a position

Logic:

Refer session  6.12 – Bitwise operators – AND, OR and XOR biwise operators to OFF a bit at a position

Program:

```#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void showbits(int);
int findbit(int,int);
int main()
{
int n,p,bit;
printf("Enter any integer:");
scanf("%d",&n);
showbits(n);
printf("\nWhich least significant bit to find:");
scanf("%d",&p);
bit=findbit(n,p);
printf("%d",bit);
return 0;
}
int findbit(int x,int pos)
{
nofb=sizeof(x)*8;
/* position must not exceeds to the number of bits */
if(pos-1>nofb)
{
printf("Position overflows..");
exit(0);
}
/* anding with mask operand */
}
void showbits(int x)
{
int i,bit,nofb;
/* sizeof bits differs platform to platform*/
nofb=sizeof(x)*8;
for(i=nofb-1;i>=0;i--)
{
bit=(x>>i)&1;
printf("%d",bit);
if(i%8==0&&i!=0)
printf("-");
}
}
```

Execution 1:
Enter any integer:425
00000000-00000000-00000001-10101001
Which least significant bit to find:4
1

Execution 2:
Enter any integer:425
00000000-00000000-00000001-10101001
Which least significant bit to find:3
0

Previous post: