Hello,
What does !! mean? as in
digitalWrite(dataPin, !!(val &(1<<i)));
else
digitalWrite(dataPin, !!(val & (1<<(7-i))));
From a for loop (i=0, i<8, I++)
This little instance of code is very confusing
Thanks
Hello,
What does !! mean? as in
digitalWrite(dataPin, !!(val &(1<<i)));
else
digitalWrite(dataPin, !!(val & (1<<(7-i))));
From a for loop (i=0, i<8, I++)
This little instance of code is very confusing
Thanks
It isn't a "!!" symbol (unlike "||" or "&&"") it is two "!" symbols.
A single exclamation point (!) is a logical not. Two exclamation points are two logical nots.
! 0 = 1
! 1 = 0
! 2 = 0
! 3 = 0
! ( ! 0 ) = ! (1) = 0
! ( ! 1 ) = ! (0) = 1
! ( ! 2 ) = ! (0) = 1
! ( ! 3 ) = ! (0) = 1
The double-not forces 0 to be 0 and non-zero to be 1.
the code is from the shiftou() function if that helps.
the code is from the shiftou() function if that helps.
CB gave you the hint: "The double-not forces 0 to be 0 and non-zero to be 1."
The second variable of a digitalWrite function is defined as a Boolean, no? Therefore it must evaluate to be a 0 or 1, not a integer evaluation that might have any value without the !! applied. At least that is my read on the subject, but I am still learning.
Lefty
The second variable of a digitalWrite function is defined as a Boolean, no?
No! It's defined as either HIGH or LOW. (I had thought it was defined as 0 or non-zero, making this code unnecessary, so I checked.) (it IS (currently) implemented as LOW or "anything else." So the use "!!" is neither correct nor necessary.)
C doesn't have a "boolean" type.
- C doesn't have a "boolean" type.
You of course may be correct, that is why I used the "no?". I've just used the Arduino reference so far learning C.
Data types in Arduino reference:
Boolean type shown in Arduino reference:
And as the OP stated the code is from the Arduino core library wiring_shift.c
Lefty
C doesn't have a "boolean" type.
It is very popular to define a type called "boolean", for clarity of operation. In arduino, it gets defined in wiring.h (which is automatically added to your sketches) typedef uint8_t boolean;
I don't think that I quite understand all the ways that such a defined type is different from a "real" boolean type, but the big one is that there is no checking done anywhere that the variable can only be assigned true and false (or 0 and 1) as values. You can happily do things like boolean myval = 33;
and the compiler won't complain (and myval will actually contain 33, rather than becoming just "true.")
and the compiler won't complain (and myval will actually contain 33, rather than becoming just "true.")
So the use "!!" is neither correct nor necessary.)
So back to the original posted question, might that be why the !! is used in the Arduino core library wiring_shift.c function to force the byte variable to be only 0 or 1 rather then a 0 or any other value? Obviously the Arduino shiftout() function compiles and works correctly.
Lefty
C might not have a generic data type BOOLEAN, but it has the concept of logical values. It has to, because a deterministic behhaviour of if/while is advantagious.
The C rules are:
0 (in case of rare one's-complement machines the positive zero): FALSE
other than 0 : TRUE
"logical" operators deliver 1 in case their value is stored
It is left to the compiler to do anything useful when the value is not stored, espacially performing optimizations on "! ! x" ( as on "- - x")
Storing a "1" is not done for the "type" but as defined by the logical operator!
The way digitalRead() handles that value is another story - see the thread about / request for "toggling" the output!
yes, it works correctly, because it just happens that LOW is defined as 0 and HIGH is defined as 1. And this is almost certainly why the shiftout() code is using this construct. When I said it "wasn't correct", I was being ... pedantic.
Would not the double exclamation point (!!) deserve to be mentioned on Arduino "Language Reference" page -- say under heading "Conversion" or "Boolean"?
This is my first posting so no links were allowed -- but I guess you got the point and found the pages or figured out the difficulty of googling for "!!", unless one finds out that one should search for "double exclamation point".
Not all have programming background and it seems one comes across "!!" notation almost everywhere while reading serious code for Arduino.
pekka
(Just another novice with Arduino)
The !! exclamation point is not a single operator, like << or ++. It is two separate operators applied in order. The ! operator is documented.
Not saying it is, just wishing to have some kind of hint at -- say -- && - Arduino Reference footnotes or threabouts pointing out why it is a clever thing to use "!!" .
pekka
But it really isn't that clever. A simple comparison to zero (!= 0) accomplishes the same thing and is easier to understand.
In other words:
Arduino is -- or I got the impression while starting with Arduino -- aimed at those who are not familiar with embedded devices or programming.
If this is true the learning curve should be made as easy as possible.
Being inquisitive leads to beginner reading and hopefully understanding what others have done, like the beginner of this thread has done (and no, I'm not saying he was also a novice) -- he found odd notation (or whatever, if that is the wrong expression) of double exclamation point and had to ask for help. If the double exclamation point would have been easily found from within the reference, this thread and discussion would have been unneccesary.
My sugestion is to add a "Hint" section to topics at reference and on
give a hint that double exclamation point is used to convert values into boolean. This is perhaps everyday routine to you advanced ethusiasts and cleary understood by consulting the refeence, but not to all beginners.
As subheadings of page:
are not links to anywhere I would have liked to see under "Conversion" one extra row under this heading:
[+] !! (to boolean)
and as the !! exclamation point is not a single operator, it should be somehow "demoted".
As on page && - Arduino Reference there is an explanation of operators whose symbols are related to chars '&', '|' and '!', and as the connection between '&' vs. '&&' and '|' vs '||' is made clear, A novice like I may easily jump into false concluson that there is a similar connection between '!' vs. '!!' (which there is not, as I now know).
I think that a simple note of this non-connection and the meaning of '!!' added on boolean page would perhaps make thigs a bit clearer to us novices.
pekka