Search This Blog

Program to Understand Bitwise Operators

This is a C program that shows working of bitwise operators. The program takes two values, a and b. And displays the different bitwise operations' results. The program performs bitwise AND, bitwise OR, bitwise XOR, bitwise inversion (NOT), right bit shift and left bit shift operations.
#include<stdio.h>
main ()
{
unsigned int a = 60; /* 60 = 0011 1100*/
unsigned int b = 13; /* 13 = 0000 1101 */
printf ("a=60 ( 0011 1100)\nb=13 ( 0000 1101)\n"); 
int c = 0;
c = a&b ; /* 12 = 0000 1100 */
printf ("a&b=%d\n",c );
c = a | b ; /* 61 = 0011 1101 */
printf ("a|b=%d\n",c );
c = a ^ b ; /* 49 = 0011 0001 */
printf ("a^b=%d\n", c );
c = ~ a; /*-61 = 1100 0011 */
printf ("~a=%d\n" , c );
c = a << 2 ; /* 240 = 1111 0000 */
printf ("a<<2=%d\n" , c );
c = a >> 2 ; /*15 = 0000 1111*/
printf ("a>>2=%d\n" , c );
}

No comments: