View previous topic :: View next topic |
Author |
Message |
PeWa
Joined: 15 Jan 2019 Posts: 22 Location: Sweden
|
The !! operator ? |
Posted: Tue Jan 15, 2019 6:12 am |
|
|
Hi guys
I have converted a short shiftout function from the Arduino forum.
It works fine shifting out bits from a byte in MSB or LSB order.
DATA is a PORT on the PIC using fast_io and #byte #bit.
CLK is another port on the PIC.
However i don't really understand the double not operator !! in the code.
I have searched the internet for an explanation but I have not find any
but the compiler don't complain and the program works as expected.
Does anyone know about this !! operator and how it works ?
Code: |
void shiftOut2(unsigned int8 val)
{
char mask = ((LSBFIRST) ? 0b10000000 : 0b00000001);
do
{
DATA = !!(mask & val);
delay_us(4);
CLK = 1;
delay_us(4);
CLK = 0;
} while ( mask = ((LSBFIRST) ? (mask >> 1) : (mask << 1)) );
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Jan 15, 2019 6:20 am |
|
|
OK I was curious...
I went to the Ardunio website and they don't list a !! operator.
I'm wondering if it's really a || operator ? Some of the fonts and colours can be 'fun' to read. I've be caught more than once as I need trifocals..sigh...
You should post a link to where this code came from, so we can see first hand what's going on.
Jay |
|
|
PeWa
Joined: 15 Jan 2019 Posts: 22 Location: Sweden
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Jan 15, 2019 7:08 am |
|
|
OK, went there (link works !), found the code...hmm, went to 'home' and then the 'ref' section...looked at 'command/operators'. NO mention of the !! operator.
Yeesh..now that's just crazy......if the guys who MAKE the compler don't list that, how are we supposed to know what it does ?
OK.. found this... NOW it kinda makes sense...
https://www.avrfreaks.net/forum/why-two-val-1
The RESULT of the {...} stuff becomes a '1' or a '0'.
I'm now thinking it might have been better presented as...
!(!(stuff));
??
Jay |
|
|
PeWa
Joined: 15 Jan 2019 Posts: 22 Location: Sweden
|
|
Posted: Tue Jan 15, 2019 8:28 am |
|
|
Ok !
Thank's for the link, now i understand more.
It´s like an OR state 0 gives 0 and 1 gives 1.
I changed the line to your suggestion and that work Ok too and is more readable.
Code: |
DATA = !(!(mask & val));
|
//PeW |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Jan 15, 2019 8:32 am |
|
|
Sure glad it makes sense now !!
I find having a lot of brackets helps me SEE what's happening.
Originally I thought !! was a weird ,unknown operator as C does have &&.
Now it seems !! is a 'quick, short cut for !(!...)
Jay |
|
|
PeWa
Joined: 15 Jan 2019 Posts: 22 Location: Sweden
|
|
Posted: Tue Jan 15, 2019 8:38 am |
|
|
Yes Exactly
It reminded me from some time back in the 90´s when a guy told me that it is nearly impossible to read a C program that is not rich commented
Maybe CCS could add a line about this in their documentation the compiler takes it Ok so it seem to be mention in the bit operator section.
//Pew |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Jan 15, 2019 9:11 am |
|
|
Ah, good old !!. That takes me back!....
This was documented very early in the development of C.
It's a standard way of converting a value to a boolean result. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 15, 2019 9:14 am |
|
|
There are many good explanations for it you can find with Google:
Quote: |
The “NOT NOT” operator or Double not(!!) operator... simply returns the
truth value of the variable or expression. |
Quote: |
If the return value of the function is greater (or less) than 0, double NOT
will produce 1. If it is 0 then double NOTing it will produce 0. |
It turns an expression into a boolean. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Jan 15, 2019 11:31 am |
|
|
Yes, it is important to understand, that unlike &&, where there is a
specific separate meaning to this compared to a single &, this is just !,
followed by another !.
The single not, returns the logical inverse of the expression to which
it is applied. Doing two one after the other simply 'not's the result of
the first one.
There is nothing here for CCS to 'document', it is not a separate operator,
just the logical operation applied twice. |
|
|
|