In: Computer Science
In C, is "&" a operator or punctuator? (According to the info found online, I am still confused)
& is a bit wise operator, where as && is a logical operator
& works on individual bits and perform bit-by-bit operation, where as && works on expressions, if both the operands are non-zero, then the condition becomes true.
For example - if we want to check a variable 'x' value is between 1 and 10 we will do it using the && operator as
if(1<=x && x<=10) ; here there are two expression 1<=x and x<=10 && computes the logical AND operation.
Now coming to & operator , lets understand with an example -
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100 ( this is the binary form of the number 60)
B = 0000 1101 ( this is the binary form of the number 13)
---------------------
A&B = ( it does bit by bit AND operation) if both the bits at the same index is 1 then the resultant bit will be 1 otherwise its always 0
so A&B will result in 0000 1100 which is equivalent to the number 12 in decimal.
so,
int A = 60;
int B = 13;
int C = A&B; will result in 12 being assigned to the variable C
======================================================================
Hope this clarifies all confusion. Let me know for any doubts.