What double negation does in C

后端 未结 5 598
谎友^
谎友^ 2021-01-14 05:34

I had a controversy about what compilers \"think\" about this:

a = 8;
b = !!a;

So, is b == 0x01 ? Is TRUE always

5条回答
  •  悲&欢浪女
    2021-01-14 06:03

    Yes, b == 1. The result of any boolean operator is always 0 or 1. You can do better though...

    I want to extract only 0x00 if (a == 0) and 0x01 if (a > 0)

    b = a > 0; most accurately reflect your rule.

提交回复
热议问题