Bitwise operators for bit manipulation in C language and example programs

Bitwise operators are very much useful in bit extraction in C language. These operators are still more useful in Embedded C and bit manipulations in C. These operations include extracting the bit status, setting the bit status, resetting the bit status etc.
Bitwise oeprators
Bitwise oeprators
Following are some of the bit manipulations that are useful.
Checking the status of bit:
  • Write a C program to check the status of bit at given position.
#include<stdio.h>  
main()  
{  
 int n,pos;  
 printf("Enter a number:");  
 scanf("%d",&n);  
 printf("Enter position:");  
 scanf("%d",&pos);  
 if(n>>pos & 1)  
 printf("Set\n");  
 else  
 printf("Reset\n");  
}  
Setting the bit:
  • Write a C program to set the bit at given position.
#include<stdio.h>  
main()  
{  
 int n,pos;  
 printf("Enter a number:");  
 scanf("%d",&n);  
 printf("Enter position to set(0 to %d):",sizeof(int)*8-1);  
 scanf("%d",&pos);  
 if(pos>31 || pos<0)  
 printf("Invalid position..\n");  
 else  
 {  
  n=n|1<<pos;  
  printf("After setting, n:%d\n",n);  
 }  
}  
In the previous program, the bug is – when the user enters beyond 0-31 range, the program does not show any information. Hence, it is modified here.
Resetting the bit:
  • Write a C program to reset the bit at given position.
#include<stdio.h>  
main()  
{  
 int n,pos;  
 printf("Enter a number:");  
 scanf("%d",&n);  
 printf("Enter position to reset(0 to %d):",sizeof(int)*8-1);  
 scanf("%d",&pos);  
 if(pos>31 || pos<0)  
 printf("Invalid position..\n");  
 else  
 {  
  n=n & ~(1 << pos);  
  printf("After resetting, n:%d\n",n);  
 }  
}  
Complementing the bit:
  • Write a C program to complement the bit at given position.
#include<stdio.h>  
main()  
{  
 int n,pos;  
 printf("Enter a number:");  
 scanf("%d",&n);  
 printf("Enter position to complement(0 to %d):",sizeof(int)*8-1);  
 scanf("%d",&pos);  
 if(pos>31 || pos<0)  
 printf("Invalid position..\n");  
 else  
 {  
  n=n ^ 1<<pos;  
  printf("After complementing, n:%d\n",n);  
 }  
}  
The following are some of the example programs using the bitwise operations in C language.
Program to exchange nibbles of a byte:
  • Write a program to exchange the nibbles of a byte.
#include<stdio.h>  
main()  
{  
 unsigned char n,temp1,temp2;  
 printf("Enter a number:");  
 scanf("%d",&n);  
 temp1=n>>4;  
 temp2=n<<4;  
 n=temp1|temp2;  
 printf("n after nibble exchange:%d\n",n);  
}  
Though n is of unsigned char type, %d is used in scanf, in order to avoid ambiguity when a character is given as input. Note that a byte of unsigned integers (0-255) is considered here.
Program to print binary equivalent of a decimal number:
  • Write a C program to print binary equivalent of a given decimal number (signed/unsigned integer) using bitwise operators.
#include<stdio.h>  
main()  
{  
 int n,i;  
 printf("Enter a number:");  
 scanf("%d",&n);  
 for(i=sizeof(int)*8-1;i>=0;i--)  
 if(n&1<<i)  
  printf("1");  
 else  
  printf("0");  
 printf("\n");  
}  
Program to check even or odd:
  • Write a C program to check if a given number is even or odd using bitwise operators.
#include<stdio.h>  
main()  
{  
 int n;  
 printf("Enter a number:");  
 scanf("%d",&n);  
 if(n&1)  
  printf("Given number is odd\n");  
 else  
  printf("Given number is even\n");  
}  
Program to swap variables:
  • Write a C program to swap two variables without using temporary variables and arithmetic operators.
#include<stdio.h>  
main()  
{  
 int a,b;  
 printf("Enter a,b:");  
 scanf("%d%d",&a,&b);  
 printf("Before swapping:\na=%d\tb=%d\n",a,b);  
 a^=b^=a^=b;     //b=(a^b)^(a=b);  
 printf("After swapping:\na=%d\tb=%d\n",a,b);  
}  
Both the logics mentioned in the program can perfectly swap two given variables without using temporary variables and arithmetic operators.
Recollect Operators in C language.
Share:

0 comments:

Post a Comment