# 13.1 – Functions on bit-wise operators part – 1

by on January 7, 2018

In the previous 4 chapters we have learned
What is function?
How a function can be defined?
How a collection of functions can be defined as header file and used commonly in different applications?
In this chapter we will develop some of the commonly used functions. These may be used as building blocks in developing other applications.

Here in this session, we will develop some functions to perform bitwise operations on data. These functions are generally used to develop low level applications.

Note: It is suggested to read and work with Bitwise operators before going further

1) Develop a function called showbits(), that accepts an integer and prints the bits?

Logic:

Now we will derive logic to extract individual digits from the data. Say for example if short type takes two bytes then binary equivalent to 425 would be 00000001 110101001.

Here in this example as the number of bits are 16 then right shifting by (16-10) 6 and anding with 00000000 00000001 (1) would give the 10th most significant bit

```x=425;
bit=(x>>6)&1;
printf("%d",bit);
```

If we want to have 13th most significant bit then right shifting by (16-13) 3 and anding with 00000000 00000001 (1) would give the required 13th most significant bit

```x=425;
bit=(x>>3)&&1;
printf("%d",bit);
```

By observing the above cases we come to know that,
if we want to get the 1st most significant bit then must be right shifted by 16-1 (15) and anding with 1
if we want to get the 2nd most significant bit then must be right shifted by 16-2 (14) and anding with 1
and if we want to get the 3rd most significant bit then must be right shifted by 16-3 (13) and anding with 1

We can generalize the above logic using a loop. Where x is the variable that holds the data, nofb is number of bits that would be taken by x and bit to store the bit at a position.

```nofb=sizeof(x)*8; /* to get number of bits */
for(i=nofb-1;i>=0;i--)
{
bit=(x>>i)&1;
printf("%d",bit);
}
```

The below logic can be used to print “-” for every 8 bits.

```if(i%8==0)
printf("-");
```

The above logic prints “-” when i is 64, 56, 48, 40, 32, 24, 16 and 8 etc.

Let us put altogether as a function

```void showbits(int x)
{
int nofb,i,bit;
for(i=nofb-1;i>=0;i--)
{
bit=(x>>i)&1;
printf("%d",bit);
if(i%8==0&&i!=0)
printf("-");
}
}
```

Program:

```#include<stdio.h>
void showbits(int);
int main()
{
int n;
printf("Enter any integer:");
scanf("%d",&n);
showbits(n);
return 0;
}
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("-");
}
}
```

Executing with gcc:
Enter any integer:425
00000000-00000000-00000001-10101001

Executing with Turbo C:
Enter any integer:425
00000001-10101001

The output varies according to the compiler we use. Here gcc prints 32 bit output because take 4 bytes for int type, Turbo C prints 16 bit output because takes 2 bytes for int type. It is compiler dependent.

2) Develop a function called onbit() to ON (1) a bit at a position

Logic:

Refer session  16.12 Biwise operators AND, OR and XOR bitwise operators to ON a bit at a position

Program:

```#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int onbit(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 ON:");
scanf("%d",&p);
n=onbit(n,p);
showbits(n);
return 0;
}
int onbit(int x,int pos)
{
int nofb=sizeof(x)*8;
/* position must not exceeds to the number of bits */
if(pos-1>nofb)
{
printf("Position overflows..");
exit(0);
}
/* oring with mask operand */
x=x|(int)pow(2,pos-1);
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 ON:15
00000000-00000000-01000001-10101001

Execution using Turbo C:
Enter any integer:425
00000001-10101001
Which least significant bit to ON:15
01000001-10101001

Previous post: