- As the image depicted above, logical and bitwise operators are used in different scene with some common features.
- To distinguish the similarities and differences does matter for better programming.
- For example a logical operator
and
is represented as &&
whereas a bitwise operator is represented as &
.
-
A logical operator expects its operands to be boolean expressions(1 or 0) and return a boolean value.
- A bitwise operator works on integral(short, int, unsigned, char, bool, long, unsigned char, etc..)values and return integral value.
#include <stdio.h>
int main(int argc, char* argv[])
{
int x = 10;
int y = 11;
if (y > 1 && y > x)
{
printf("y is greater than 1 AND x\n");
}
int z = x & y;
printf("z-> x bitwise(AND) y \n%d", z);
printf("\n");
return 0;
}
~