Don't understand operator as used in a statement.

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.

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.

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. :wink:

I am not the greatest coder, still in training but I understood that

!=

Means not equal to.. it is not (something)
I have never seen it like this

= !

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 "="

think of it this way:

ledState = (!ledState);

That I have seen, and use it lots actually in my video game programming

if (!GetIsObjectInvalid)
{do stuf}

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.

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.

Jack3M:
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.

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


!!0 = 0

Where would one ever use this single statement and why? Sometimes C is just too cute for it's own good. :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.

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)

Where would one ever use this single statement

Nowhere. You can't assign a value to the result of an operation.

PaulS:

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

Would the compiler allow
int value = !!!!0;

Yes, of course.

(I found the use in the cores - it's in "wiring_shift.c")

AWOL:

Would the compiler allow
int value = !!!!0;

Yes, of course.

(I found the use in the cores - it's in "wiring_shift.c")

Looks like software masturbation to me. :wink:
And just another reason software geeks don't interface well with the general population. :wink:

And just another reason software geeks don't interface well with the general population.

Hey! I resemble that remark.

PaulS:

And just another reason software geeks don't interface well with the general population.

Hey! I resemble that remark.

I know you do. :smiley:

I know you do. :slight_smile:

Ouch!