Any reason for this: !!

J

Jonathan Bartlett

I recently see the following code:

bool b = !!(flags & 0x200);

Any reason for the double ! ?

To force conversion to a boolean probably. flags & 0x200 will give an
integer result. "!" will convert it to a boolean, but the opposite
truth value, and the next "!" will convert it to the original value.

Not sure that it's actually needed, but that's at least probably what
they were thinking. They basically did that instead of shifting the bit
to the first position.

Jon
 
J

josue.gomes

Hi,

I recently see the following code:

bool b = !!(flags & 0x200);

Any reason for the double ! ?

Josue
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

josue.gomes said:
Hi,

I recently see the following code:

bool b = !!(flags & 0x200);

Any reason for the double ! ?

Yes. And if you think about it a bit, you'll see the logic.

If you need help, see the truth table below

x !x !(!x)
=== === ====
-2 0 1
-1 0 1
0 1 0
1 0 1
2 0 1

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCXSjvagVFX4UWr64RAvuDAKCrrPsxmJT1Fqj9XsLDx26ZMr5BFACfYuyU
Sn7hqw579jdQfm6o2mTnDH4=
=hQA0
-----END PGP SIGNATURE-----
 
D

Darius

josue.gomes said:
Hi,

I recently see the following code:

bool b = !!(flags & 0x200);

Any reason for the double ! ?

Josue

bool can only be 1 or 0

lets say flags = 0x2200
(flags & 0x200) = 0x200
then !(flags & 0x200) = 0
and !!(flags & 0x200) = 1

hope you get it !!!
 
C

Clark S. Cox III

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)
).
 
K

Keith Thompson

Clark S. Cox III said:
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.

Only if the "bool" in the program is the macro defined in C99's
<stdbool.h>. (Yes, it's a macro, not a typedef.)

If it's a user-defined boolean type other than _Bool, either in a
pre-C99 program or in a program that doesn't include <stdbool.h>, the
conversion doesn't normalize the result to 0 or 1 and the "!!" is
potentially useful.
 
A

Alex Vinokur

josue.gomes said:
Hi,

I recently see the following code:

bool b = !!(flags & 0x200);

Any reason for the double ! ?

Josue

!! returns 0 or 1 only.

By the way !!(flags & 0x200) is equal to (flags >> 9) & ~(~0 << 1))
 
P

Peter Nilsson

Alex said:
!! returns 0 or 1 only.

By the way !!(flags & 0x200) is equal to (flags >> 9) & ~(~0 <<
1))

No, it isn't.

~0 may be a trap representation; and if 'flags' is negative, then the
result
of (flags >> 9) is implementation defined.

Of course, if 'flags' is negative, then (flags & 0x200) is basically
unspecified
anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top