Why '&&' and not '&'?

前端 未结 16 1457
别跟我提以往
别跟我提以往 2020-11-27 12:11

Why is && preferable to & and || preferable to |?

I asked someone who\'s been programming for years a

相关标签:
16条回答
  • 2020-11-27 12:58
    if (list.Count() > 14 && list[14] == "foo")
    

    is safe

    if (list.Count() > 14 & list[14] == "foo")
    

    would crash if the list doesn't have the right size.

    0 讨论(0)
  • 2020-11-27 13:03

    In most cases, && and || are preferred over & and | because the former are short-circuited, meaning that the evaluation is canceled as soon as the result is clear.

    Example:

    if(CanExecute() && CanSave())
    {
    }
    

    If CanExecute returns false, the complete expression will be false, regardless of the return value of CanSave. Because of this, CanSave is not executed.

    This is very handy in the following circumstance:

    string value;
    if(dict.TryGetValue(key, out value) && value.Contains("test"))
    {
        // Do Something
    }
    

    TryGetValue returns false if the supplied key is not found in the dictionary. Because of the short-circuiting nature of &&, value.Contains("test") is only executed, when TryGetValue returns true and thus value is not null. If you would use the bitwise AND operator & instead, you would get a NullReferenceException if the key is not found in the dictionary, because the second part of the expression is executed in any case.

    A similar but simpler example of this is the following code (as mentioned by TJHeuvel):

    if(op != null && op.CanExecute())
    {
        // Do Something
    }
    

    CanExecute is only executed if op is not null. If op is null, the first part of the expression (op != null) evaluates to false and the evaluation of the rest (op.CanExecute()) is skipped.

    Apart from this, technically, they are different, too:
    && and || can only be used on bool whereas & and | can be used on any integral type (bool, int, long, sbyte, ...), because they are bitwise operators. & is the bitwise AND operator and | is the bitwise OR operator.

    To be very exact, in C#, those operators (&, | [and ^]) are called "Logical operators" (see the C# spec, chapter 7.11). There are several implementations of these operators:

    1. For integers (int, uint, long and ulong, chapter 7.11.1):
      They are implemented to compute the bitwise result of the operands and the operator, i.e. & is implement to compute the bitwise logical AND etc.
    2. For enumerations (chapter 7.11.2):
      They are implemented to perform the logical operation of the underlying type of the enumeration.
    3. For bools and nullable bools (chapter 7.11.3 and 7.11.4):
      The result is not computed using bitwise calculations. The result is basically looked up based on the values of the two operands, because the number of possibilities is so small.
      Because both values are used for the lookup, this implementation isn't short-circuiting.
    0 讨论(0)
  • 2020-11-27 13:04

    C# Operators should explain why:

    Essentially having two &'s or |'s means that it is a conditional rather than a logical, so you can tell the difference between the two.

    & Operator has an example of using one &.

    0 讨论(0)
  • 2020-11-27 13:05

    To explain very clearly what this means (even though the other answers hint at it - but probably use terminology you don't understand).

    The following code:

    if (a && b)
    {
       Foo();
    }
    

    Is really compiled to this:

    if (a)
    {
        if (b)
        {
            Foo();
        }
    }
    

    Where the following code is compiled exactly as it is represented:

    if (a & b)
    {
       Foo();
    }
    

    This is called short-circuiting. In general you should always use && and || in your conditions.

    Bonus Marks: There is one scenario when you shouldn't. If you are in a situation where performance is crucial (and this is nano-seconds crucial) only use short-circuiting when you must (e.g. null checking) - as a short-circuit is a branch/jump; which could result in a branch-misprediction on your CPU; an & is much cheaper than &&. There is also a scenario where short-circuiting can actually break logic - have a look at this answer of mine.

    Diatribe/Monologue: Regarding the branch mis-prediction that most blissfully ignore. Quoting Andy Firth (who has been working on games for 13 years): "This may be lower level that people think they need to go... but they'd be wrong. Understanding how the hardware you're programming for treats branches can affect performance to a HUGE degree... far more than most programmers may appreciate re: death by a thousand cuts."

    • Game developers (and others working in extreme real-time conditions) go as far as restructuring their logic to better suit the predictor. There is also evidence of this in decompiled mscorlib code.
    • Just because .NET shields you from this type of thing doesn't mean it's not important. A branch mis-prediction is horribly expensive at 60 Hz; or at 10,000 requests/second.
    • Intel wouldn't have tools to identify the location of mis-predictions, nor would Windows have a performance counter for this, nor would there be a word to describe it, were it not a problem.
    • Ignorance about the lower levels and architecture does not make someone who is aware of them wrong.
    • Always try to understand the limitations of the hardware you are working on.

    Here is a benchmark for the non-believers. It's best to run the process in RealTime/High to mitigate the scheduler having an effect: https://gist.github.com/1200737

    0 讨论(0)
  • 2020-11-27 13:05

    && and & mean two very different things and give you two different answers.

    1 && 2 yields 1 ("true")
    1 & 2 yields 0 ("false")

    && is a logic operator -- it means "true if both of the operands are true"
    & is a bitwise comparison. It means "tell me which of the bits are set in both of the operands"

    0 讨论(0)
  • 2020-11-27 13:07

    If you are an old-timer C programmer, be careful. C# has really surprised me.

    MSDN says for the | operator:

    Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.

    (Emphasis is mine.) Boolean types are handled specially, and in this context the question only starts to make sense, and the difference is, as other already expained in their answers:

    && and || are short-circuiting. & and | evaluate both operands.

    and what's preferable depends on many things like side-effects, performance and code readability, but generally the short-circuiting operators are preferable also because they are better understood by people with a similar background like me.

    The reason is: I would argument like this: Since there is no real boolean type in C, you could use the bitwise operator | and have its result evaluated as truthy or falsy in an if condition. But this is the wrong attitude for C#, because there is already a special case for boolean types.

    0 讨论(0)
提交回复
热议问题