473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Any reason for this: !!

Hi,

I recently see the following code:

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

Any reason for the double ! ?

Josue
Nov 14 '05 #1
7 1094

josue.gomes wrote:
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 !!!
--------------------
darius

Nov 14 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

josue.gomes wrote:
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)

iD8DBQFCXSjvagVFX4UWr64RAvuDAKCrrPsxmJT1Fqj9XsLDx2 6ZMr5BFACfYuyU
Sn7hqw579jdQfm6o2mTnDH4=
=hQA0
-----END PGP SIGNATURE-----
Nov 14 '05 #3
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

Nov 14 '05 #4
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
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #5
Clark S. Cox III <cl*******@gmail.com> writes:
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

"josue.gomes" <jo******************@gmail.com> wrote in message news:b5**************************@posting.google.c om...
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))

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Nov 14 '05 #7
Alex Vinokur wrote:
"josue.gomes" <jo******************@gmail.com> wrote:
Hi,

I recently see the following code:

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

Any reason for the double ! ?


!! 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.

--
Peter

Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alexandros Frantzis | last post by:
Hello! Is there a standard way to find the reason a file stream open failed (eg file doesn't exist, access rights)? Thanks.
10
by: Jag | last post by:
can anyone tell me what is SQL0911 reason code 68 thanks jag
1
by: Stefano Tesini | last post by:
Hi all, when i drop a table, i take his error : SQL0902C System error (REASON CODE = "122"). Why ? Ver UDB Server nt 7.1.0 Ver UDB Cli 7.1.0 Win 98 or XP the error is the same.
3
by: Eveline | last post by:
Dear all, I have a database on which I do stuff like: - creating tables - filling those tables with data - joining tables - EXPORT of queries to file - LOAD data from file into a tables
4
by: acemann7 | last post by:
Windows 2000 udb v 7.2 Got the latest fixpak 13 Applied it. Still can't get a single java sp to run. I used to get reason "2"... now reason "0" - reason 0 is not even documented. I point...
1
by: Ted | last post by:
Hello, Is there an equivalent to die(reason) in ASP.NET? I have a ValidateData() function that returns a boolean, so instead of If condition Then Response.Writeline(reason): Return False ...
9
by: shorti | last post by:
db2 V8.2 I have been looking for a good way to log the 'reason code' when we hit an sqlcode that uses them. From what I can tell the reason code is stored in sqlca.sqlerrmc. But, there seems...
8
by: Challenge | last post by:
Hi, I got error, SQL1768N Unable to start HADR. Reason code = "7", when I tried to start hadr primary database. Here are the hadr configuration of my primary db: HADR database role ...
2
by: BD | last post by:
Hi, all. I'm trying to implement a REFRESH IMMEDIATE MQT to help with performance of a particularly sluggish query. I cannot create it with REFRESH IMMEDIATE, because of reason code "10",...
16
by: John Salerno | last post by:
Just something that crosses my mind every time I delve into "Learning Python" each night. Does anyone see any value in learning Python when you don't need to for school, work, or any other reason?...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.