Blogger templates

Pages

Saturday, 28 April 2012

Explanation of Bitwise C Operator with Examples

C bitwise operator

Bitwise operator:
  • x=x<<2   means new value of x is shifted left by 2 positions
  • y=y>>3   means new value of y is shifted right by 3 positions
Program of bitwise operator:

#include<stdio.h>
void main()
{
int x,y;
x=128;                             
y=32;

x=x>>1;
printf(" After right- shifting by 1, x=%d/n" ,x);
y=y<<2;
printf(" After left- shifting by 2, y=%d/n" ,y);
}             // End of main() //       

              

Output:
 /* Binary value of x before shifting :

10000000

Binary value of x after shifting right by 1 :

01000000
*/
 

After right-shifting by 1, x=64                       // 64 is equivalent to 01000000 binary number //

/* Binary value of y before shifting :

100000

Binary value of y after shifting right by 2 :

001000
*/

After left-shifting by 2, y=128             // 128 is equivalent to 001000 binary number //


0 comments:

Post a Comment