Difference between & and && in C?

后端 未结 3 927
北恋
北恋 2020-12-21 03:56

What is the difference between & and && in C?

My teacher gave me this example:

int a = 8;
int b = 4;
printf(\"a         


        
3条回答
  •  猫巷女王i
    2020-12-21 04:32

    The & operator performs a bit-wise and operation on its integer operands, producing an integer result. Thus (8 & 4) is (0b00001000 bitand 0b00000100) (using a binary notation that does not exist in standard C, for clarity), which results in 0b00000000 or 0.

    The && operator performs a logical and operation on its boolean operands, producing a boolean result. Thus (8 && 4) is equivalent to ((8 != 0) and (4 != 0)), or (true and true), which results in true.

提交回复
热议问题