Pages: [1] 2   Go Down
Author Topic: Don't understand operator as used in a statement.  (Read 638 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I noticed in the "debounce" code a statement that was not in the book, and I cannot find any reference anywhere else of similar statement and though slightly documented, I don't understand what is happening to the variable that is changed back and forth from HIGH to LOW

The snippet is thus
 // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }

The " = !"  By simple equal and not, I am assuming that  ledState is being changed to NOT HIGH, but what is it becoming?  Null?

Very sorry if this is unintelligent question, but really want to understand what is happening to the value of ledState in that line of code.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49071
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The " = !"
Is actually two separate operators. The statement is assigning the value of !ledState to ledState. The ! operator (NOT) simply changes true to false or false to true.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The = and ! are not together, but rather two separate operators (there is a space between them). The = just assigns the value on the right to the left. The ! says if the ledState is currently LOW then pass a High to the left of the =, or the opposite. So ! just means negate the value it is attached to. I've used it mostly with boolean variables as I haven't played with what it does to int variables.
So in this example ledState is just flipped from low to high or high to low every time it executes.

Lefty

Edit: Drat, software people are always quicker for these kinds of questions.  smiley-wink
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 179
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not the greatest coder, still in training but I understood that
Code:
!=
Means not equal to.. it is not (something)
I have never seen it like this
Code:
= !
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 300
Posts: 26219
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's an assignment (=), followed by a NOT (!).
It isn't a logical operator like "!=", and it isn't a compound assignment (>>=, +=, -= etc), where the operator comes before the "="
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 33
Posts: 1434
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

think of it this way:
Code:
ledState = (!ledState);
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 179
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That I have seen, and use it lots actually in my video game programming
Code:
if (!GetIsObjectInvalid)
{do stuf}
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1434
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not exactly.
I was just showing with the parens that the ! operator is totally separate from the assignment operator.

It is a quick way to toggle ledState.

Though the ! operator is used exactly the same. It negates ledState, the = then assigns this new value to ledState.

So if ledState is false, !ledState is true, and that is placed back into ledState so that after the statement, ledState is true.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting, so this only works in a true/false statement?    I sorta assumed it was flipping HIGH and LOW but don't grasp what is happening at the microcontroller level, especially if it was working with numbers instead of keywords.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting, so this only works in a true/false statement?    I sorta assumed it was flipping HIGH and LOW but don't grasp what is happening at the microcontroller level, especially if it was working with numbers instead of keywords.

It does flip HIGH and LOW, but only because HIGH is defined as 1 and LOW is defined as 0. Which just happen to be the values of true and false respectively. There is no guarantee that this will always be the case in future releases of the IDE, so you will see advice that suggests you should not rely on it. I'd be rather surprised if it ever did change though.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 300
Posts: 26219
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
especially if it was working with numbers instead of keywords.
It only ever works with numbers - on the microcontroller, there's nothing else!

Since the beginning, C has always had the convention that zero is false, and anything else (positive or negative) is true.
The  NOT ("!") operator can usefully be used in pairs to ensure that something that is simply "true" is C terms, is reduced to a single bit.
e.g.
!42 = 0
!0 = 1
so, !!42 = 1
and
!!0 = 0
Code:
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
!!0 = 0

Where would one ever use this single statement and why? Sometimes C is just too cute for it's own good.  smiley-wink

 There were times in hardware designs where we would utilize two series wired invert gates, but that was to increase fanout drive for the signal.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 300
Posts: 26219
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Where would one ever use this single statement and why?
You would obviously never use it with a constant like that, but if you look hard enough, the construct is used at least once in the Arduino cores.
(Though I admit the the actual usefulness in that instance is reduced because the context in which it is used reduces to the classic "false...else" internally anyway)
« Last Edit: December 19, 2013, 08:39:41 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49071
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Where would one ever use this single statement
Nowhere. You can't assign a value to the result of an operation.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Where would one ever use this single statement
Nowhere. You can't assign a value to the result of an operation.

Well I was assuming it's usage would be like

int value = !!0;

And of course that is pretty useless (and somewhat confusing), but probably logically correct. I'm just suprized the compiler would except doubling up by using multiple !.

Would the compiler allow
int value = !!!!0; and give the same results. Seems to me it would just add more complexity to the compiler (optimizer burden?) then the designers would allow for?

Lefty 
Logged

Pages: [1] 2   Go Up
Jump to: