Skip to contentSkip to conversation

A pettiness question.

DISCUSSION Nick Warne 6 participants 6 messages Sep 21, 2005 – Sep 22, 2005

lore
Share & tools
lore

Nick Warne

author
#0 lore raw
Quoted 6 lines
Quoted 4 lines

This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.

Am I right?

Yes. I think of it as a "truth value" predicate (or operator).

Interesting. I thought maybe this way was trick, until later I experimented.

My post here (as Bill Stokes):

http://www.quakesrc.org/forums/viewtopic.php?t=5626

So what is the reason to doing !!num as opposed to num ? 1:0 (which is more readable I think, especially to a lesser experienced C coder). Quicker to type?

My quick test shows compiler renders both the same?

Nick

Signature · 2 lines
"When you're chewing on life's gristle,
Don't grumble, Give a whistle..."
#1 lore raw
Quoted 9 lines · Nick Warne
Quoted 6 lines
Quoted 4 lines

This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.

Am I right?

Yes. I think of it as a "truth value" predicate (or operator).

Interesting. I thought maybe this way was trick, until later I experimented.

Quoted 7 lines

My post here (as Bill Stokes):

http://www.quakesrc.org/forums/viewtopic.php?t=5626

So what is the reason to doing !!num as opposed to num ? 1:0 (which is more readable I think, especially to a lesser experienced C coder). Quicker to type?

Some people also prefer the following form:

	num != 0
Quoted 7 lines

My quick test shows compiler renders both the same?

Nick
--
"When you're chewing on life's gristle,
Don't grumble, Give a whistle..."
-

-Vadim Lobanov

#2 lore raw
Quoted 19 lines · Vadim Lobanov
Quoted 15 lines · Nick Warne
Quoted 9 lines
Quoted 6 lines

This give a enum of {0,1}. If test is not 0, !!test will give 1, otherwise 0.

Am I right?

Yes. I think of it as a "truth value" predicate (or operator).

Interesting. I thought maybe this way was trick, until later I experimented.

My post here (as Bill Stokes):

http://www.quakesrc.org/forums/viewtopic.php?t=5626

Quoted 10 lines
Quoted 5 lines

So what is the reason to doing !!num as opposed to num ? 1:0 (which is more readable I think, especially to a lesser experienced C coder). Quicker to type?

Some people also prefer the following form:

	num != 0

That one looks good for if-tests and such. But if you need a 0 or 1 for adding to a counter, then

a += !!x;

looks much better than

a += (x != 0);

There is nothing special about !!. Just learn to use it, it is easy to understand/read, and saves typing.

Perhaps a "C-contructs FAQ" might be useful, for the benefit of newbies and beginners. It could document things like the use of !!, common kernel macro tricks, when not to use zero initializers, and so on.

Helge Hafting

#3 lore raw
Quoted 9 lines · Helge Hafting

That one looks good for if-tests and such. But if you need a 0 or 1 for adding to a counter, then

a += !!x;

looks much better than

a += (x != 0);

Actually I prefer:

a += (x == '-'-'-'?'-'-'-':'/'/'/');

:)

-- Steve

#4 lore raw
Quoted 13 lines · Steven Rostedt
Quoted 9 lines · Helge Hafting

That one looks good for if-tests and such. But if you need a 0 or 1 for adding to a counter, then

a += !!x;

looks much better than

a += (x != 0);

Quoted 7 lines

Actually I prefer:

a += (x == '-'-'-'?'-'-'-':'/'/'/');

:)

-- Steve

I like that! Nevertheless, for readability one should probably standardize upon something returing 1 or 0 for a true/false comparison. I see "(x)?1:0" a lot in the bit-banging code and I'm pretty sure the compiler knows how to optimize it.

The bang/bang stuff was a way of hiding warnings back in the days that lint was used as a code-checker. It's use should be condemned to the fullest extent!

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13 on an i686 machine (5589.55 BogoMips). Warning : 98.36% of all statistics are fiction.

****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

#5 lore raw
In reply to Nick Warne
Quoted 11 lines · Nick Warne

Interesting. I thought maybe this way was trick, until later I experimented.

My post here (as Bill Stokes):

http://www.quakesrc.org/forums/viewtopic.php?t=5626

So what is the reason to doing !!num as opposed to num ? 1:0 (which is more readable I think, especially to a lesser experienced C coder). Quicker to type?

I think using !! is quick and the place where it is used, will look little bit odd (like you say in #define or macros) if some one use num ? 1 : 0 ...... And I think lesser experienced C coder will learn other ways of doing same things !!!!!

Quoted 2 lines

My quick test shows compiler renders both the same?

Ya, I think both !! and num ? 1: 0 will result in same thing by compiler

--
Fawad Lateef