Subj : Re: Really **wonderful** website for C interview questions!!!!! To : comp.programming From : Scott Moore Date : Mon Jul 11 2005 12:28 pm Robert Bunn wrote: > "Willem" wrote in message > news:slrndcj08i.atb.willem@toad.stack.nl... > >>Robert wrote: >>) >>) "Willem" wrote in message >>) news:slrndcgmak.2e1g.willem@toad.stack.nl... >>)> Robert wrote: >>)> ) Okay. I guess I *am* a novice. How is >>)> ) >>)> ) if(x == (!!y) ) >>)> ) any more useful (or clear) than >>)> ) >>)> ) if(x && y) >>)> >>)> Well, for starters those two statements don't have the same > > meaning, > >>)> even for boolean values... >>)> >>) >>) Well, yes. You're right. I realized after sending I should've used >>) >>) if ( (!!x) == (!!y) ) >>) >>) for the first case. Does that make the question I asked answerable? >> >>No. They still don't have the same boolean-logic meaning. >>Why don't you write out a truth-table for '&&' and '==' ? >> >> > > > Okay. I see it now. (!!x) == (!!y) works out to !(x^y) > Guess I *reallY* don't get the job now. *sheesh* > > Anyway, can I have a hint? *Is* there some use for "!!"? > > -- > Rob > > extern volatile int bitreg; void setbit(int b) { bitreg |= !!b << 5; } A common idiom in hardware programming for "set bit in external device". The !! operation cleans up the truth value b. In the case of this routine, you are making sure that the caller didn't just stick an odd value in there like: int *p; setbit(p); For "true if the pointer is not zero", so it's defensive programming. Another example is: extern volatile int reg1, reg2; reg2 |= !!(reg1 & 0x40) << 5; Meaning "transfer bit 6 of reg1 to bit 5 or reg2". Some argue that reg1 != 0 is clearer, so its up to you. I use it because its short, and (I admit) because I think its funny to see peoples reactions when they see it. But that's just me. .