A Bitwise And operator is represented as ‘&’ and a logical operator is represented as ‘&&’. The following are some basic differences between the two operators.
a) The logical and operator ‘&&’ expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value.
The bitwise and operator ‘&’ work on Integral (short, int, unsigned, char, bool, unsigned char, long) values and return Integral value.
y is greater than 1 AND x z = 3
b) If an integral value is used as an operand for ‘&&’ which is supposed to work on boolean values, the following rule is used in C.
…..Zero is considered as false and non-zero is considered as true.
For example in the following program x and y are considered as 1.
It is compiler error to use the non-integral expression as operand for bitwise &. For example the following program shows compiler error.
error: invalid operands to binary & (have 'float' and 'float')
c) The ‘&&’ operator doesn’t evaluate the second operand if the first operand becomes false. Similarly ‘||’ doesn’t evaluate the second operand when first operand becomes true. The bitwise ‘&’ and ‘|’ operators always evaluate their operands.
0 Geeks in & 0
The same differences are there between logical OR ‘||’ and bitwise OR ‘|’.
This article is contributed by Ujjwal Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes