ternary vs double exclamation

Valdis.Kletnieks at vt.edu Valdis.Kletnieks at vt.edu
Mon Dec 29 20:00:08 EST 2014


On Mon, 29 Dec 2014 22:25:41 -0200, Vinícius Tinti said:

> I was looking the kernel source code and there are a lot of places in
> which either "(expression) ? 1 : 0" or "(expression) ? 0 : 1" appear.
> As fair as I can tell both can be replaced by "!!expression" and
> "!expression".

As far as the compiler goes, they're the same thing, as you already discovered.

Some are just code written by not-so-experts.

In other cases, the author may have wanted to keep clear that we were
calculating an integer to be used for further arithmetic rather than a boolean.

Patches to clean it up wouldn't be a bad idea in some cases.  However, each
location will have to be examined for what was intended and what produces more
readable code.  If you're *really* ambitious, converting stuff to use a boolean
rather than an int where appropriate would be nice - we don't do that enough
currently. But that will require actually reading and understanding the code.
That's not a good task for those who like to submit patches without
thinking....

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20141229/8bd32c46/attachment.bin 


More information about the Kernelnewbies mailing list