CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

The !! operator ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PeWa



Joined: 15 Jan 2019
Posts: 22
Location: Sweden

View user's profile Send private message

The !! operator ?
PostPosted: Tue Jan 15, 2019 6:12 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 6:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 6:25 am     Reply with quote

Ok it used here in several examples for the Arduino Shiftout function
the code i have used is from lloyddean / scroll down a bit.

http://forum.arduino.cc/index.php?topic=45529.0
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 7:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 8:28 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 8:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 8:38 am     Reply with quote

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 Smile

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

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 9:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 9:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 15, 2019 11:31 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group