On 2005-04-13 10:11:32 -0400,
jo******************@gmail.com
(josue.gomes) said:
Hi,
I recently see the following code:
bool b = !!(flags & 0x200);
Any reason for the double ! ?
In this case (i.e. when converting an integer value to bool), no. Those
two "!" operators end up doing what the conversion would have done
implicitly.
Each of the following produce the same value:
(bool)!!(flags & 0x200);
(bool)(flags & 0x200) != 0;
(bool)(flags & 0x200)?1:0;
(bool)(flags & 0x200);
(bool)!!!!!!!!!!!!(flags & 0x200);
In C89, before bool/_Bool existed, it can be useful to force a value to
either 1 or 0. I've come across code that uses when indexing
multidimensional arrays used as lookup tables, where some of the
dimensions were [2], and were looked up by boolean flags:
int table[2][] = { ... };
#define FETCH_VALUE_FROM_TABLE(flag,index) (table[!!(flag)][index])
In this case, the "!!" is to assure that (flag) is interpreted as a
boolean (Though were I writing it, I would have preferred ((flag) != 0)
).
--
Clark S. Cox, III
cl*******@gmail.com