Page 1 of 1

AND function in ESP32! SOLVED! Use && instead

Posted: Mon Mar 08, 2021 7:42 pm
by jgu1
Hi!

I am playing with a project with an Ili9341 and an ESP32 with XPT2046

If I use an IF contain this: X > 34 AND X < 130 AND Y > 110 AND Y < 65 I get an error when compiling look like the ESP32 don´t like AND. It work with arduino and Arm I also try with &

Is there other command for AND in ESP32?

****** SOLVED***** Search in the net, USE && :D :D

Thank´s in advance

Jorgen

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Mon Mar 08, 2021 9:24 pm
by medelec35
Hi, Jorgen.
Just to clarify, You always use && instead of AND in any decisions when comparing values.
&& is Logical.
AND is bitwise.
Also for when comparing values use the following logical operators:
|| = OR (| is the pipe key next to Z on a UK keyboard).
! = NOT

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Tue Mar 09, 2021 6:29 pm
by jgu1
Hi MArtin!

Thank you very much. Learn some new today :D

So AND versus is not equal to && I have earlyer used OR and AND in Arduino and Pic whith good luck.

Thank´s again I will be aware in the future ;)

Br Jorgen

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Tue Mar 09, 2021 11:42 pm
by medelec35
Hi Jorgen,
Your'e welcome.
jgu1 wrote:
Tue Mar 09, 2021 6:29 pm
So AND versus is not equal to &&
No definitely not the same.
The AND OR NOT etc for bit wise manipulation only.

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Wed Mar 10, 2021 10:06 am
by Steve-Matrix
The bitwise AND (&) can sometime appear to work like the logical &&, but not always. I think it's always safer to avoid "AND", "OR", etc., and always use the correct symbol (e.g. & or &&) to avoid confusion.

For example, consider three byte variables (X=1, Y=2, Z=3) in the following decisions:

"if (X & Y)" is false, but "if (X & Z)" is true (the bitwise operator results in "if 0" and "if 1" respectively).

"if (X && Y)" is true, and "if (X && Z)" is true (X, Y and Z are all non-zero and converted to "true" when using the logical operators).

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Thu Mar 11, 2021 5:57 pm
by jgu1
Hi Martin and Steve!

Thank you very much. Yes , I though it doesn´t matter how to use ;) And as Steve said it sometimes work I Expeince it work with AND OR ect. It have always used this. I am glad to know your explanation and of course, from now always use you advice.

But please tell me, why sometime use two &,|.

Eks:

If A & B =2

If A && B =2

Br Jorgen

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Thu Mar 11, 2021 6:14 pm
by LeighM
Google C operators 😊

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Fri Mar 12, 2021 9:34 am
by Steve-Matrix
jgu1 wrote:
Thu Mar 11, 2021 5:57 pm
But please tell me, why sometime use two &,|.
Single is boolean operation: "10 & 7" => 2

Double is a logical operation: "true && false" => false

A further complication is that in logical operations (e.g. in the decision icon) any non-zero number is converted to "true" and zero is converted to "false". So "if (10 & 7)" becomes "if (2)" which becomes "if (true)".

But "if (10 & 5)" becomes "if (0)" which becomes "if (false)".

But Leigh is correct - there are lots of better explanations elsewhere on the internet!

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Fri Mar 12, 2021 10:06 am
by LeighM
This one is very good, which explains its top ranking :)
https://www.tutorialspoint.com/cprogram ... rators.htm

Re: AND function in ESP32! SOLVED! Use && instead

Posted: Fri Mar 12, 2021 3:22 pm
by jgu1
Thank you Leigh. As Steve said Google it ,I am in full speed and begin to understand :D

I will have a look at your link, thank you and have a nice weekend.

Br Jorgen