Bitwise Operators

Bitwise operators work on bits and perform the bit-by-bit operations.

Bitwise operations consists of two digits 0 and 1.

The Bitwise operators are :

& Binary AND
| Binary OR
^ Binary XOR
~ Binary One's Complement
>> Right Shift
<< Left Shift

Bitwise operations program

    
#include <stdio.h>

int main()
{
    int a = 12;             /* 12 = 0000 1100 */
    int b = 25;             /* 25 = 0001 1001 */
    int c = 0;
            
    //Performs Binary AND operation
    c = a & b;
    printf("Binary AND value of c is %d\n", c );
            
    //Performs Binary OR operation
    c = a | b;
    printf("Binary OR value of c is %d\n", c );
            
    //Performs Binary XOR operation
    c = a ^ b;
    printf("BInary XOR value of c is %d\n", c );
            
    //Performs Binary 1's Complement operation
    c = ~a;
    printf("1's complement value of c is %d\n", c );
            
    //Performs Left Shift operation
    c = a << 2;
    printf("Left Shift value of c is %d\n", c );
            
    //Performs Right Shift operation
    c = a >> 2;
    printf("Right Shift value of c is %d\n", c );
            
    return 0;
}       
    

Output

        
Binary AND value of c is 8
Binary OR value of c is 29
Binary XOR value of c is 21
1's complement value of c is -13
Left Shift value of c is 48
Right Shift value of c is 3
        
    

Post a Comment

0 Comments