Skip to main content

examples of bitwise operator in c

#include <stdio.h>
 
int main()
{
   int m = 40,n = 80,AND_appropriate,OR_appropriate,XOR_appropriate,NOT_appropriate ;
   AND_appropriate = (m&n);
   OR_appropriate = (m|n);
   NOT_appropriate = (~m);
   XOR_appropriate = (m^n);
   printf("AND_appropriate value = %d\n",AND_appropriate);
   printf("OR_appropriate value = %d\n",OR_appropriate );
   printf("NOT_appropriate value = %d\n",NOT_appropriate );
   printf("XOR_appropriate value = %d\n",XOR_appropriate );
   printf("left_shift value = %d\n"m << 1);
   printf("right_shift value = %d\n"m >> 1);
}
/*consider x=40 and y=80. Binary form of these values are given below.

x = 00101000
y=  01010000

All bit wise operations for x and y are given below.

x&y = 00000000 (binary) = 0 (decimal)
x|y = 01111000 (binary) = 120 (decimal)
~x = 11111111111111111111111111 11111111111111111111111111111111010111 = -41 (decimal)
x^y = 01111000 (binary) = 120 (decimal)
x << 1 = 01010000 (binary) = 80 (decimal)
x >> 1 = 00010100 (binary) = 20 (decimal)
Bit wise left shift and right shift : In left shift operation “x << 1 “, 
1 means that the bits will be left shifted by one place.
 If we use it as “x << 2 “,  then, it means that the bits will be left shifted by 2 places.
*/

Comments

Popular posts from this blog

example of typecasting in c

/*Type Casting in C Typecasting allows us to convert one data type into other. */ //simple example to cast int value into the float. #include <stdio.h>    int   main (){   float   f = ( float ) 9 / 4 ;     printf ( "f : %f \n " ,  f  );     return   0 ;   }