By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

Oring multiple values

P: n/a
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.

- James
Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
James Aguilar wrote:
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.


Assuming each 'fnX' returns some type for which conversion to 'bool' is
defined or the operator| is defined (like an integral rvalue), there is no
difference between the two approaches. I'd probably use the latter simply
because it's all in one line and therefore easier to understand.

V
Jul 23 '05 #2

P: n/a
* James Aguilar:
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.


In that case, perhaps

bool const test = !!(fn1() + fn2() + fn3() + fn4());

which can not be misread as shortcut evaluation, and less probably
"fixed" changing "|" to "||" by some maintainance programmer.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3

P: n/a
Alf P. Steinbach wrote:
* James Aguilar:
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.

In that case, perhaps

bool const test = !!(fn1() + fn2() + fn3() + fn4());

which can not be misread as shortcut evaluation, and less probably
"fixed" changing "|" to "||" by some maintainance programmer.


With the only problem that if fn<odd> return -1 and fn<even> return 1,
it has a rather strange outcome, don't you think?

V
Jul 23 '05 #4

P: n/a
Appreciated.
Jul 23 '05 #5

P: n/a
* Victor Bazarov:
Alf P. Steinbach wrote:
* James Aguilar:
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.

In that case, perhaps

bool const test = !!(fn1() + fn2() + fn3() + fn4());

which can not be misread as shortcut evaluation, and less probably
"fixed" changing "|" to "||" by some maintainance programmer.


With the only problem that if fn<odd> return -1 and fn<even> return 1,
it has a rather strange outcome, don't you think?


We must assume that those are 'bool' functions, "even if it returns false".
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6

P: n/a

"Victor Bazarov" <v.********@comAcast.net> schrieb im Newsbeitrag
news:Bh*******************@newsread1.mlpsca01.us.t o.verio.net...
James Aguilar wrote:
Suppose that I want to get the or of several values. Which of these ways would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the time -- in other words, even if it returns false, it is useful.


Assuming each 'fnX' returns some type for which conversion to 'bool' is
defined or the operator| is defined (like an integral rvalue), there is no
difference between the two approaches. I'd probably use the latter simply
because it's all in one line and therefore easier to understand.


The two approaches are different: in your first approach every fnX() will be
executed, in the
last one the execution of fnX depends on the result of fn(X-1). We careful
with that.
Jul 23 '05 #7

P: n/a

"Real Name" <re@na.me> schrieb im Newsbeitrag
news:36*************@individual.net...

"Victor Bazarov" <v.********@comAcast.net> schrieb im Newsbeitrag
news:Bh*******************@newsread1.mlpsca01.us.t o.verio.net...
James Aguilar wrote:
> Suppose that I want to get the or of several values. Which of these ways > would be better?
>
> bool test = false;
> test = fn1() || test;
> test = fn2() || test;
> test = fn3() || test;
> test = fn4() || test;
>
> Or
>
> bool test = fn1() | fn2() | fn3() | fn4();
>
> Assume that each fn type function does stuff that needs to be done all the > time -- in other words, even if it returns false, it is useful.


Assuming each 'fnX' returns some type for which conversion to 'bool' is
defined or the operator| is defined (like an integral rvalue), there is
no
difference between the two approaches. I'd probably use the latter
simply
because it's all in one line and therefore easier to understand.


The two approaches are different: in your first approach every fnX() will
be
executed, in the
last one the execution of fnX depends on the result of fn(X-1). We careful
with that.


You may have missed that in the first version the OP used 'logical or' while
in the second version he used 'bitwise or'. Shortcut evaluation is performed
only in the case of 'logical or'. So if all those functions return a bool
result, both versions are indeed identical.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #8

P: n/a
James Aguilar wrote:
Suppose that I want to get the or of several values. Which of these ways would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();


I think I prefer the second, but as long as you're only dealing with
four functions, it probably doesn't make a lot of difference which you
use. If four functions is basically just an example, and it might
really be 20 functions instead, you might want to consider
alternatives. The first obvious one would be to create an array of
pointers to functions, and write a loop to invoke them in turn.

It would probably also be possible to create a template to handle this,
but I have a hard time imagining it being used enough to justify
putting much work into it.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #9

P: n/a
On Tue, 01 Feb 2005 17:20:14 -0600, James Aguilar wrote:
Suppose that I want to get the or of several values. Which of these ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all the
time -- in other words, even if it returns false, it is useful.

- James


Beware of bitwise or'ing function values. I ran into a problem once where
code was running differently on DOS vs. the Macintosh. It turns out that
the evaluation order of the '|' operands was different for the two
compilers, so the functions were being called in different order.

FYI

- Jay

Jul 23 '05 #10

P: n/a
Karl Heinz Buchegger wrote:
"Real Name" <re@na.me> schrieb im Newsbeitrag
news:36*************@individual.net...
"Victor Bazarov" <v.********@comAcast.net> schrieb im Newsbeitrag
news:Bh*******************@newsread1.mlpsca01.us .to.verio.net...
James Aguilar wrote:

Suppose that I want to get the or of several values. Which of these


ways
would be better?

bool test = false;
test = fn1() || test;
test = fn2() || test;
test = fn3() || test;
test = fn4() || test;

Or

bool test = fn1() | fn2() | fn3() | fn4();

Assume that each fn type function does stuff that needs to be done all


the
time -- in other words, even if it returns false, it is useful.

Assuming each 'fnX' returns some type for which conversion to 'bool' is
defined or the operator| is defined (like an integral rvalue), there is
no
difference between the two approaches. I'd probably use the latter
simply
because it's all in one line and therefore easier to understand.


The two approaches are different: in your first approach every fnX() will
be
executed, in the
last one the execution of fnX depends on the result of fn(X-1). We careful
with that.

You may have missed that in the first version the OP used 'logical or' while
in the second version he used 'bitwise or'. Shortcut evaluation is performed
only in the case of 'logical or'. So if all those functions return a bool
result, both versions are indeed identical.


Is there a guarantee of evaulation order in the second case? If not,
and the fnX() have side effects, then there is a difference. Yes, the
order of the bitwise operator is defined, but as I understand it, the
compiler is free to evaluate fnX in any order it wants, so long as the
bitwise-or operators are done in the proper order.

Jul 23 '05 #11

P: n/a
red floyd wrote:
Karl Heinz Buchegger wrote:
"Real Name" <re@na.me> schrieb im Newsbeitrag
news:36*************@individual.net...
"Victor Bazarov" <v.********@comAcast.net> schrieb im Newsbeitrag
news:Bh*******************@newsread1.mlpsca01.us.t o.verio.net...

James Aguilar wrote:

> Suppose that I want to get the or of several values. Which of these
ways

> would be better?
>
> bool test = false;
> test = fn1() || test;
> test = fn2() || test;
> test = fn3() || test;
> test = fn4() || test;
>
> Or
>
> bool test = fn1() | fn2() | fn3() | fn4();
>
> Assume that each fn type function does stuff that needs to be done all
the

> time -- in other words, even if it returns false, it is useful.
Assuming each 'fnX' returns some type for which conversion to 'bool' is
defined or the operator| is defined (like an integral rvalue), there
is no
difference between the two approaches. I'd probably use the latter
simply
because it's all in one line and therefore easier to understand.
The two approaches are different: in your first approach every fnX()
will be
executed, in the
last one the execution of fnX depends on the result of fn(X-1). We
careful
with that.

You may have missed that in the first version the OP used 'logical or'
while
in the second version he used 'bitwise or'. Shortcut evaluation is
performed
only in the case of 'logical or'. So if all those functions return a bool
result, both versions are indeed identical.


Is there a guarantee of evaulation order in the second case? If not,
and the fnX() have side effects, then there is a difference. Yes, the
order of the bitwise operator is defined, but as I understand it, the
compiler is free to evaluate fnX in any order it wants, so long as the
bitwise-or operators are done in the proper order.


Whether "ORing" is done "in the right order", doesn't really matter, there
can be no overflow. You're absolutely right, if there are side effects,
the result can be different.

V
Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.