Bitwise Operators


Bitwise Operators

In C, the next 6 operators are bitwise operators (operating at a lower level)
& (Bitwise AND) in C or C ++ takes two numbers as operators and does ONLY for all two numbers. The AND result is only 1 if both pieces are 1.

The | (bitwise OR) in C or C ++ takes two numbers as operators and makes OR in all two numbers. The result of OR is 1 if any of these two components is 1.

The ^ (bitwise XOR) in C or C ++ takes two numbers as operators and makes XOR in both numbers. The XOR result is 1 if the two pieces are different.

<< (Shift left) to C or C ++ takes two numbers, the left shifts the fragments of the first operand, the second operand determines the number of locations to be removed.

>> (right shift) in C or C ++ takes two numbers, the right removes fragments of the first operand, the second operand determines the number of locations to be removed.

~ (Bitwise NOT) in C or C ++ takes one number and converts all its bits

Example:

/* A program to show various bitwise operation */ #include<stdio.h> #include<conio.h> void main() { int a= 40, b=80,AND_xyz,OR_xyz,XOR_xyz,NOT_xyz; AND_xyz = (a&b); OR_xyz = (a|b); XOR_xyz = (~a); NOT_xyz = (a^b); printf("AND_xyz value=%d\n",AND_xyz); printf("OR_xyz value=%d\n",OR_xyz,); printf("XOR_xyz value=%d\n",XOR_xyz,); printf("NOT_xyz value=%d\n",NOT_xyz,) printf("left_shift value=%d\n",a<<1); printf("right_shift value=%d\n",a>>1); getch(); }

Output:

    AND_xyz value=0
    OR_xyz value=120
    XOR_xyz value=-41
    NOT_xyz value=120
    left_shift value=80
    right_shift value=20