'AND' vs '&&' as operator

后端 未结 10 845
野性不改
野性不改 2020-11-22 02:42

I have a codebase where developers decided to use AND and OR instead of && and ||.

I know that there is a

10条回答
  •  半阙折子戏
    2020-11-22 03:31

    Let me explain the difference between “and” - “&&” - "&".

    "&&" and "and" both are logical AND operations and they do the same thing, but the operator precedence is different.

    The precedence (priority) of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator.

    Mixing them together in single operation, could give you unexpected results in some cases I recommend always using &&, but that's your choice.


    On the other hand "&" is a bitwise AND operation. It's used for the evaluation and manipulation of specific bits within the integer value.

    Example if you do (14 & 7) the result would be 6.

    7   = 0111
    14  = 1110
    ------------
        = 0110 == 6
    

提交回复
热议问题