Pages: [1] 2   Go Down
Author Topic: !! Symbol  (Read 2858 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

It isn't a "!!" symbol (unlike "||" or "&&"") it is two "!" symbols.

http://arduino.cc/en/Reference/Boolean
« Last Edit: August 19, 2010, 03:07:58 pm 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.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12904
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

[size=14]  ! 0 = 1
  ! 1 = 0
  ! 2 = 0
  ! 3 = 0

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


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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the code is from the shiftou() function if that helps.
Logged

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

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

Lefty
« Last Edit: August 19, 2010, 09:22:37 pm by retrolefty » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 133
Posts: 6749
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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
« Last Edit: August 20, 2010, 01:13:06 am by retrolefty » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 133
Posts: 6749
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
boolean myval = 33;
and the compiler won't complain (and myval will actually contain 33, rather than becoming just "true.")
Logged

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

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
« Last Edit: August 20, 2010, 03:21:20 am by retrolefty » Logged

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: August 20, 2010, 04:37:49 am by mpeuser » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 133
Posts: 6749
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
« Last Edit: November 25, 2010, 09:13:40 am by pekka » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49272
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The !! exclamation point is not a single operator, like << or ++. It is two separate operators applied in order. The ! operator is documented.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 25, 2010, 11:25:27 am by pekka » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12904
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Pages: [1] 2   Go Up
Jump to: