-1

Normally when we use a conditional logical operator such as && and || we can use it as such:

if (x > y && y > z)
    //codes

OR

if (x > y || x > z)
    //codes

But when I was reading a page in MSDN, they mentioned that we can use & and |. I tested, and it does compile without giving any compilation errors.

So when shall we use a single logical operator like & and | instead of && and || ?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • 1
    While it's a duplicate, there are some very subtle implications to the decision to not short-circuit, which means there's definitely enough meat to this question that it's a good one. – Jon Hanna Jun 22 '18 at 15:22

2 Answers2

3

The single ones are bitwise operators, while the double ones are logical ones:

  • If you work with bool, stick to && and ||.
  • If you want to do bit arithmetic, use & and |.

For details on bitwise operators, see Wikipedia, e.g.:

5 & 3 => 1

The reason for this is that 1 is the only power of 2 shared by 5 and 3:

  101 (5)
& 011 (3)
  -------
  001 (1)

Hope this helps.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • 1
    Wrong. What `&` and `|` actually are depends on the operands. The question is clearly about the boolean `&` and `|`. – H H May 27 '14 at 17:02
1

The double Boolean operators are short circuiting operators; meaning that it will not check other operand if the first failed; this is considered more efficient

Edit:

Some downvoters stimulated me to add this part,

Although & operator is bitwise operator, however it can be used as logical operator in which case both operand will be evaluated without short circuiting

Referring to MSDN : http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx

"Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true."

So the question was asking implicitly about the difference between the operators that both can do the job but he wanted to know which to choose

The question as I understood was to differentiate between (&) and (&&) as logical operators, not to list the functionalities of each separately

Therefore I express my objection on the abuse of downvoters

stackunderflow
  • 3,811
  • 5
  • 31
  • 43
  • 1
    While your statement is true about short circuiting, your answer is incorrect for the OP's question. `&` and `|` or *bitwise* operators and operate in a completely different manner than `&&` and `||`. – Tim May 26 '14 at 19:10
  • @Tim thank you for taking the time to explain why downvote, however I don't agree and I think the downvoters missed the context of the question – stackunderflow May 26 '14 at 20:04
  • 2
    I didn't downvote - if I had, I would have said so (I don't believe in anyonymous downvotes). OP asked: "So when shall we use a single logical operator like & and | instead of && and || ?", which your *original* answer didn't seem to really address. Your edit does in a much more complete manner. – Tim May 26 '14 at 20:08
  • 1
    @stackunderflow I hope someone would lift off the down votes on my post as well. I can give you a +1 to neutralise whoever down voted you. If only someone could do the same to my post as well ! :-( `+1 from me` – user3437460 May 26 '14 at 20:42
  • 1
    @Tim: It is your comment that is incorrect for this particular case. `&` and `|` perform bitwise arithmetic on integral types, but here they are used on booleans. For booleans, short circuiting is the ONLY difference. – Ben Voigt May 26 '14 at 21:11
  • 1
    @BenVoigt - I did not know they operated on booleans until the answer was edited (I thought they always did bitwise arithmetic, as that was the only use case I ever had for them). In light of the expanded information I did upvote the answer, and this is actually a better answer overall than the one selected (IMO). – Tim May 26 '14 at 21:13
  • _Although & operator is bitwise operator_ is contradicted by the quotes below it. What the `&` _is_ depends on the operands. It can be address-of as well. – H H May 27 '14 at 07:42