42

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?

WilliamKF
  • 41,123
  • 68
  • 193
  • 295
  • apart from writing clear code, conversion bool<->int can be a performance issue. – Gene Bushuyev Jul 05 '11 at 02:27
  • I think *case1* and *case2* have the same values, and the bitwise AND of bools is portable. – Cacho Santa Jul 05 '11 at 02:32
  • Just for more information, one of the operators that is implementation-defined(according to K&R book) is the right-shift of signed values. – Cacho Santa Jul 05 '11 at 02:40
  • 1
    Your example makes them identical. But calling these as `foo() & bar()` vs `foo() && bar()` will have different effects - in the latter case, if foo() returns false, bar() (which can start an elevator on top of returning bool) will never be executed. – SF. Jul 05 '11 at 09:53
  • Related: [Boolean values as 8 bit in compilers. Are operations on them inefficient?](https://stackoverflow.com/q/47243955) re: some missed optimizations from actual compilers, like GCC, clang, and MSVC. And cases where they're successful. – Peter Cordes Mar 17 '22 at 04:04

7 Answers7

46

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.

Nemo
  • 70,042
  • 10
  • 116
  • 153
  • 1
    do your arguments apply? to | versus || Thanks. – PatrickT Nov 20 '15 at 04:24
  • 3
    @PatrickT: Yes, and for the same reasons. ("OR" and "AND" are dual operations; that is, if you swap "OR" with "AND" and "false" with "true", all equations still hold. Put another way, OR and AND are really the same operation, just living in positive-logic and negative-logic worlds. Their implementation in C/C++ respects this similarity, which is called "De Morgan's laws".) – Nemo Dec 17 '15 at 02:46
17

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.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
8

There is a difference (well, two), though you wouldn't see it in your example.

"&" does a bitwise "AND" operation, meaning that 0x1 & 0x1 = 0x1, but 0x1 & 0x2 = 0x0. OTOH, "&&" is a boolean/logical "AND", meaning it treats any non-zero value as a TRUE, so 0x1 && 0x1 = TRUE (which is generally represented as -1, ie, all ones [or maybe it's represented as 1 in C++, I forget]), while 0x1 && 0x2 = TRUE as well.

In addition, "&&" is short-circuiting, meaning that if the first operand is FALSE, the second won't be evaluated. So, while FALSE & null_pointer->booleanField ==> null pointer exception, FALSE && null_pointer->booleanField = FALSE.

There may be a slight performance advantage in using bitwise operations in some cases, but generally you should use the double forms when evaluating boolean values, so that your code is independent of the precise representation of boolean TRUE and FALSE.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Can there ever be a bool with a value of 0x2 or is that prohibited by the standard (or even a meaningless question)? – usr Oct 18 '12 at 23:18
  • The C/C++ standard specifies that `true` has the integer value 1. But if you, eg, invoke a method that returns a Boolean, there's always a danger that whoever implemented it didn't read the standard. – Hot Licks Oct 19 '12 at 00:19
  • I just ran into the `0x1 & 0x2 = 0x0` case in the wild. I had a function `funct1()` that was supposed to return true, but actually had no return statement. Don't know why that didn't generate a compiler error, but it was actually returning an undefined integer cast as a bool. So when I did `bool ok = funct1(); ok &= funct2();` I was getting incorrect results because ok appeared to be true after the first statement in the watch window, but was actually an even number, which when bitwise anded with an actual "true" in the second statement became false. – Denise Skidmore Feb 25 '15 at 23:35
5

"&&" is a "conditional logical "AND" it evaluates the second expression ONLY if the first is TRUE

"&" is a "non-conditional logical AND" <-- (if you are playing with boolean expressions) it evaluates both expression


MOREOVER "&" is a 'bitwise' operator that means it operates on a bit-level.

This example can make you understand better.

4 = 00000100  // 'four' bit set
5 = 00000101  // 'four' bit and 'one' bit set

00000100 (4) & // AND: only keep bits set in both
00000101 (5)
--------
00000100 (4)

00000100 (4) | // OR: keep bits set in either
00000101 (5)
--------
00000101 (5)

00000100 (4) ^ //  EXCLUSIVE OR: keep bits only set in one but not the other
00000101 (5)
--------
00000001 (1)
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
makiSTB
  • 87
  • 3
  • 7
4

Algorithmically there's no difference, however use of && allows you to "short circuit" the check. That is, to decide case2, if val1 is false then the compiled code has no reason to check the value of val2 in order to determine the answer, where case1 requires the actual AND to take place.

Realistically, a good compiler will recognize this and produce the same code... it comes down to how good your compiler is.

mah
  • 39,056
  • 9
  • 76
  • 93
0

The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise.

The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves.

Javeria
  • 17
  • 1
-2
int a = 0, b = 10;

if(a == 1 && (b/a) == 0) cout << "This is okay\n"; // the 2nd expression is never checked as the first one is false

cout << "Bingo\n";

if(a == 1 & (b/a) == 0) cout << "This is not okay\n"; // program crashes here trying to divide by zero

cout << "Bingo\n"; // this will never get printed
Shuvo Sarker
  • 187
  • 9
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Jun 08 '17 at 07:46