Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between && and & with bool(s)?

In C++, is there any difference between doing && (logical) and & (bitwise) between bool(s)?

bool val1 = foo(); bool val2 = bar();  bool case1 = val1 & val2; bool case2 = val1 && val2; 

Are case1 and case2 identical or if not how exactly do they vary and why would one choose one over the other? Is a bitwise and of bools portable?

like image 465
WilliamKF Avatar asked Jul 05 '11 02:07

WilliamKF


People also ask

Are there any difference or is there any difference?

Which One Should You Use: Is There A or Is There Any? We must use 'a' with singular countable nouns and 'any' with uncountable nouns. We use 'is' with both singular countable nouns and uncountable nouns.

Does it make any different or difference?

When asking for anything different you are asking for a particular thing that changed. When asking for any difference then you can refer to something that changed but not particularly to a specific thing. anything different between the two you are asking between a particular thing(s) being different.

Which is correct different or difference?

Difference vs Different The only difference between these words is in its usage in English grammar. Difference is the noun, whereas different is an adjective.

What is the difference between on and in with examples?

IN Use in when something is located inside of a defined space. It could be a flat space, like a yard, or a three-dimensional space, like a box, house, or car. The space does not need to be closed on all sides (“There is water IN the glass”). ON Use on when something is touching the surface of something.


2 Answers

The standard guarantees that false converts to zero and true converts to one as integers:

4.7 Integral conversions

...

If the destination type is bool, see 4.12. If the source type is bool, the value false is converted to zero and the value true is converted to one.

So the effect in the example you give is guaranteed to be the same and is 100% portable.

For the case you give, any decent compiler is likely to generate identical (optimal) code.

However, for Boolean expressions expr1 and expr2, it is not true in general that expr1 && expr2 is the same as expr1 & expr2 because && performs "short-circuit" evaluation. That is, if expr1 evaluates to false, expr2 will not even be evaluated. This can affect performance (if expr2 is complicated) and behavior (if expr2 has side-effects). (But note that the & form can actually be faster if it avoids a conditional branch... Toying with this sort of thing for performance reasons is almost always a bad idea.)

So, for the specific example you give, where you load the values into local variables and then operate on them, the behavior is identical and the performance is very likely to be.

In my opinion, unless you are specifically relying on the "short-circuit" behavior, you should choose the formulation that most clearly expresses your intention. So use && for logical AND and & for bit-twiddling AND, and any experienced C++ programmer will find your code easy to follow.

like image 72
Nemo Avatar answered Sep 28 '22 09:09

Nemo


When using logical and &&, the right-hand expression will not be evaluated if the left-hand expression is false.

A lot of C/C++/C# code relies on this, as in: if (p != null && p->Foo()).

For your example I would use case2 (logical and). Only use bitwise when dealing with bit flags, etc.

However, if foo() and bar() only return bool (0, 1) then case1 and case2 are the same.

like image 30
Richard Schneider Avatar answered Sep 28 '22 10:09

Richard Schneider