-2

For the below sample code:

// Note that a, b, c and d can have value of 0 or 1 only
int isAllTrue(int a, int b, int c, int d)
{
   return (a && b && c && d);

   // THIS ALSO RETURNS CORRECT VALUE
   // return (a & b & c & d);
}

If we know that operands can be either 0 or 1, which operation would be preferable from performance point of view: bitwise or logical? Does it really matter?

Does evaluation stop in the case of bitwise "&" when the operand is 0 ?

Raviraj
  • 7
  • 1
  • In case of `&&` evaluation stops if processor finds one value is false. – sjsam Jul 22 '16 at 07:11
  • What do you mean by *preferable*? There are many answers to this question... – Purag Jul 22 '16 at 07:12
  • Possible duplicate of [Java: Are bitwise OR and AND FASTER than the equivalent logical operators?](http://stackoverflow.com/questions/11052362/java-are-bitwise-or-and-and-faster-than-the-equivalent-logical-operators) – Thomas Betous Jul 22 '16 at 07:12
  • 1
    Possible to duplicate : http://stackoverflow.com/questions/24542/using-bitwise-operators-for-booleans-in-c –  Jul 22 '16 at 07:15
  • 1
    Have a look at [\[ this \]](http://stackoverflow.com/a/6577545/1620779) answer. – sjsam Jul 22 '16 at 07:19
  • 1
    `(10 & 5 & 3) = 0`, `(10 && 5 && 3) = 1` so the '*bitwise*' and '*conditional and*' expressions are not *interchangeable*. – David C. Rankin Jul 22 '16 at 07:31
  • 1
    @DavidC.Rankin Question is asking about case where operands are known to be 0 or 1. – Markus Laire Jul 22 '16 at 07:38
  • 1
    @tbetous StackOverflow centered around tags. Duplicates cannot be closed using a tag for a different language. Therefore in the future please respect this when proposing closing as duplicate questions – 2501 Jul 22 '16 at 07:43
  • 1
    @User123 Please read above comment. – 2501 Jul 22 '16 at 07:44
  • Make your code readable by adding space characters around operators. – gnasher729 Jul 22 '16 at 08:42
  • sometimes `&` is faster because there's no branch misprediction – phuclv Jul 22 '16 at 08:57
  • @LưuVĩnhPhúc: Sometimes `&&` is faster because there is less code executed. So what? – too honest for this site Jul 22 '16 at 09:16
  • you guys obviously do not even know what the difference between logical and bitwise operators are ... they cannot be compared, they are not interchangeable, they are completely different language concepts - comparing or interchanging them is like comparing `if` and `public static` or something ... does not compute – specializt Jul 22 '16 at 09:32
  • I get the impression he understands the difference between logical and bitwise operators. In the case given, since the operands are all known to be 1 or 0, either operator will give the same result. One might be faster than the other, and I believe that is what he is asking. – Jeff Loughlin Jul 22 '16 at 14:06

3 Answers3

1

Use & for bit operations. Use && for logical operations. If you use a bitwise operation where you should use a logical operation or vice versa, the reader will (a) be confused, (b) check very carefully if your code has introduced a subtle bug.

Programming relies on idioms. If your code doesn't match the expected idiom, then you must have a good reason, and you should write a comment explaining why.

Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
gnasher729
  • 51,477
  • 5
  • 75
  • 98
-1

It doesn't matter which formulation you use, with active compiler optimization, all shown evaluations should already be completed at compile time.

As for your last question, the bitwise operators do not employ shortcut evaluation and thus always evaluate each operand.

Peter G.
  • 14,786
  • 7
  • 57
  • 75
-3

there seems to be a lot of confusion about questions like this one ... and a lot of wrong answers. Well, here are the facts for C/C++ :

  • &&, || and ! are logical operators, they test for boolean expressions - integral values (numbers) are considered true if their value is not zero, false otherwise - no matter how many numbers you check for but 0 && 1 will be evaluated to false, for instance because of the first 0 -- it could be re-written as false && true
  • &, |, ~, ^, >>, << are bitwise operators, they mask/assign bit patterns of integral values, you can do a lot of cool calculations like that - but you really need to know how numbers are represented inside of your CPU before you start doing that.

Conclusion : your question doesnt make sense - because you thought that these operators are similar .... they arent.

specializt
  • 1,913
  • 15
  • 26
  • The question is perfectly sensible for every implementation that uses all zero-bits to represent the integer 0, which is...most of them. In fact, the only reasonable exception might be implementations using signed-magnitude representations, but even there, the declaration shown presumably creates "positive" 0, which is bitwise still all-zeroes. So instead of lecturing about the distinctions between the operator types (which it would appear the OP already understood), why not examine the edge cases and/or subtleties of bit representation? – Kyle Strand Jul 25 '16 at 04:10
  • *"you really need to know how numbers are represented inside of your CPU before you start doing that*" - Incidentally, you don't. You need to know the rules established by the programming language you use. The representation in hardware isn't interesting (although it's usually compatible with the rules of the programming language). – IInspectable Aug 14 '16 at 08:01