A clear, layman's explanation of the difference between | and || in c#?

后端 未结 11 550
余生分开走
余生分开走 2020-12-02 12:51

Ok, so I\'ve read about this a number of times, but I\'m yet to hear a clear, easy to understand (and memorable) way to learn the difference between:

if (x |         


        
11条回答
  •  一个人的身影
    2020-12-02 13:45

    The first, bitwise operator works on two numerical values and results in a third one.

    If you have binary variables

    a = 0001001b;
    b = 1000010b;
    

    then

    a | b == 1001011b;
    

    That is, a bit in the result is 1 if it is also 1 in either of the operands. (My example uses 8-bit numbers for clarity's sake)

    The "double pipe" ||, is a logical OR operator that takes two boolean values and results in a third.

提交回复
热议问题