Go Down

### Topic: !! Symbol (Read 5266 times)previous topic - next topic

#### psppb

##### Aug 19, 2010, 09:45 pm
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

#### AWOL

#1
##### Aug 19, 2010, 10:04 pmLast Edit: Aug 19, 2010, 10:07 pm by AWOL Reason: 1
It isn't a "!!" symbol (unlike "||" or "&&"") it is two "!" symbols.

http://arduino.cc/en/Reference/Boolean
"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.

#2
##### Aug 19, 2010, 10:08 pm

A single exclamation point (!) is a logical not.  Two exclamation points are two logical nots.

[font=Courier New][size=14]  ! 0 = 1
! 1 = 0
! 2 = 0
! 3 = 0

! ( ! 0 ) = ! (1) = 0
! ( ! 1 ) = ! (0) = 1
! ( ! 2 ) = ! (0) = 1
! ( ! 3 ) = ! (0) = 1
[/size][/font]

The double-not forces 0 to be 0 and non-zero to be 1.

#### psppb

#3
##### Aug 20, 2010, 04:04 am
the code is from the shiftou() function if that helps.

#### retrolefty

#4
##### Aug 20, 2010, 04:21 amLast Edit: Aug 20, 2010, 04:22 am by retrolefty Reason: 1
Quote
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

#### westfw

#5
##### Aug 20, 2010, 07:46 am
Quote
The second variable of a digitalWrite function is defined as a Boolean, no?

1) 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.)

2) C doesn't have a "boolean" type.

#### retrolefty

#6
##### Aug 20, 2010, 08:06 amLast Edit: Aug 20, 2010, 08:13 am by retrolefty Reason: 1
Quote
2) 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:
http://arduino.cc/en/Reference/HomePage

Boolean type shown in Arduino reference:

http://arduino.cc/en/Reference/BooleanVariables

And as the OP stated the code is from the Arduino core library wiring_shift.c

Lefty

#### westfw

#7
##### Aug 20, 2010, 10:01 am
Quote
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)
Code: [Select]
`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
Code: [Select]
`boolean myval = 33;` and the compiler won't complain (and myval will actually contain 33, rather than becoming just "true.")

#### retrolefty

#8
##### Aug 20, 2010, 10:19 amLast Edit: Aug 20, 2010, 10:21 am by retrolefty Reason: 1
Quote
and the compiler won't complain (and myval will actually contain 33, rather than becoming just "true.")

Quote
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

#### deSilva

#9
##### Aug 20, 2010, 11:28 amLast Edit: Aug 20, 2010, 11:37 am by mpeuser Reason: 1
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!

#### westfw

#10
##### Aug 20, 2010, 04:43 pm
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.

#### pekka

#11
##### Nov 25, 2010, 02:57 pmLast Edit: Nov 25, 2010, 03:13 pm by pekka Reason: 1
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)

#### PaulS

#12
##### Nov 25, 2010, 04:16 pm
The !! exclamation point is not a single operator, like << or ++. It is two separate operators applied in order. The ! operator is documented.

#### pekka

#13
##### Nov 25, 2010, 05:24 pmLast Edit: Nov 25, 2010, 05:25 pm by pekka Reason: 1
Not saying it is, just wishing to have some kind of hint at  -- say -- http://arduino.cc/en/Reference/Boolean footnotes or threabouts pointing out why it is a clever thing to use "!!" .
pekka

#14
##### Nov 25, 2010, 08:16 pm

But it really isn't that clever.  A simple comparison to zero (!= 0) accomplishes the same thing and is easier to understand.

Go Up

Please enter a valid email to subscribe