& or && ???

Hi folks,

I know: & is for bit-operations and && is boolean, but can someone tell me the the difference between:

if (a > b) & (b==HIGH)
do something;

and

if (a > b) && (b==HIGH)
do something;

seemes as if both are working properly...

Maybe someone can tell me whether both expressions are allowed? or what's the difference between this two expressions.

Thankx + Best regards from good old Germany!

Elvis01

Looks the same to me?

& is a bit wise and operator (ie., b101 & b100 becomes b100)

&& is the logical and operator

Using & in and if statement may or may not evaluate correctly when you really mean AND (or &&) depends on the data types and compiler options.

adwsystems:
Looks the same to me?

SORRY! I was wrong, now it is correct! :-))

If then statements are a condition and a response. && is the question. "Do I have 1 && 1?" & is an operation. "a = b & c;" There will almost never be a situation where you want & in a condition statement. To add to confusion, & is also a reference operator, which is beyond the scope of your question.

Perehama:
If then statements are a condition and a response. && is the question. "Do I have 1 && 1?" & is an operation. "a = b & c;" There will almost never be a situation where you want & in a condition statement. To add to confusion, & is also a reference operator, which is beyond the scope of your question.

adwsystems:
Looks the same to me?

& is a bit wise and operator (ie., b101 & b100 becomes b100)

&& is the logical and operator

Using & in and if statement may or may not evaluate correctly when you really mean AND (or &&) depends on the data types and compiler options.

OK, thank you!
seemes I was only lucky, that the compiler interprets it in a correct way. So it is better to use && in if statements!

Elvis01:
OK, thank you!
seemes I was only lucky, that the compiler interprets it in a correct way. So it is better to use && in if statements!

In preferences, set warnings to ALL and you may see that the compiler knows the difference.

In many conditionals, & will appear to act the same as && because of the way C represents true (non zero, or one) and false (zero).

Thus "a > b" evaluates to 1 or 0, and "b == HIGH" also evaluates to 1 or 0. The two results are bitwise ANDed, and the final result is also 1 or 0.

HOWEVER, the && operator guarantees that if the left-hand expression evaluates to false, the right-hand expression is not evaluated at all, something that the & operator does not.

Elvis01:
OK, thank you!
seemes I was only lucky, that the compiler interprets it in a correct way. So it is better to use && in if statements!

No the compiler didn't interpret it any way. What happened is that you got lucky and the math worked out the same. It would be like if you were supposed to add two numbers and you multiplied instead but you got lucky because the numbers were both 2 and adding and multiplying gave the same answer.

Elvis01:
I know: & is for bit-operations and && is boolean, but can someone tell me the the difference between:

if (a > b) & (b==HIGH) 

do something;



and


if (a > b) && (b==HIGH)
 do something;



seemes as if both are working properly...

Neither will work properly. You are missing a pair of parentheses around the conditional expression. Like this:

if ((a > b) && (b==HIGH))
 do something;

It's a little odd that you are comparing 'a' to 'b'. Comparing 'b' to HIGH implies that 'b' is the result of a digitalRead() (or will be used for a digitalWrite()) in which case it should only be one of two values:
LOW/0/false or HIGH/1/true. It is odd to compare such a value to anything other than a similar value that can only be LOW or HIGH. If 'a' is such a value, the (a > b) expression will only be true if a==HIGH and b==LOW. But then you also test 'b' to make sure it is HIGH. If 'a' is a variable containing HIGH or LOW, the expression can never be true. If 'b' is LOW it fails the second half of the expression and if 'b' is HIGH it fails the first half.

Elvis01:
Hi folks,

I know: & is for bit-operations and && is boolean, but can someone tell me the the difference between:

if (a > b) & (b==HIGH)
do something;

and

if (a > b) && (b==HIGH)
do something;

In the Programming School, I have learnt like these --
1. & symbol is used to mean bit-wise logical AND operation at the individual bit levels of two integer type operands (variables). For example:
(a) Simple Bit Masking

byte x1 = 0b101010101;
byte x2 = 0b0101011;
byte x3 = x1 & x2;  //x3 = 00000001

(b) If() Control Structure

if (x1 & x2)   //ANDing at individual bit levels; if corresponding bits of any position found 1s, the argument stands as 1 
{
   Serial.println("Argument of if() structure stands for HIGH(1)!"); //both bits of Position-0 are 1s.
}
else
{
   Serial.println("Argument of if() structure stands for LOW(0)!");
}

2. && symbol(s) is used to mean expression-wise logical AND of two operands which may be dissimilar at their own levels. For example:
(a)

#define HIGH 1
bool a = 1;
bool b = 0;

void setup() 
{
  Serial.begin(9600);
 
  if((a>b) && (b==HIGH))  //1st expression stands 1; 2nd expression stands 0; argument stands for 0
  {
    Serial.println("Argument of if() structure stands for HIGH(1)!"); 
  }
  else
  {
    Serial.println("Argument of if() structure stands for Low(0)!");
  }
}

void loop() 
{

}

(b)

if((a>b) & (b==HIGH))  //the argument is not constructed as per definition of & symbol. 
//& expects integer operands. Are (a>b) and (b==HIGH) integer operands?
{

}
else
{

}[code]

[/code]

johnwasser:
Neither will work properly. You are missing a pair of parentheses around the conditional expression. Like this:

if ((a > b) && (b==HIGH))

do something;





It's a little odd that you are comparing 'a' to 'b'. Comparing 'b' to HIGH implies that 'b' is the result of a digitalRead() (or will be used for a digitalWrite()) in which case it should only be one of two values:
LOW/0/false or HIGH/1/true. It is odd to compare such a value to anything other than a similar value that can only be LOW or HIGH. If 'a' is such a value, the (a > b) expression will only be true if a==HIGH and b==LOW. But then you also test 'b' to make sure it is HIGH. If 'a' is a variable containing HIGH or LOW, the expression can never be true. If 'b' is LOW it fails the second half of the expression and if 'b' is HIGH it fails the first half.

Hi johnwasser,

thank you for your answer. Yes you are absolutly right. I ve tried to make an example, but it was not perfect:
First I forgot the parantheses and second b==HIGH should be more like c==HIGH.

I've learned a lot from all the answers, A BIG THANK YOU TO ALL !!