C 语言 Double Negation (!!) 运算符

C/C++ 中所有逻辑运算符(logical operator)中,其操作数(operand)都是以 非零 去判断的。例如,对一元的逻辑取反(negation)! 运算符而言:

printf("%d\n", !0); // 1
printf("%d\n", !1); // 0
printf("%d\n", !2); // 0 (这个比较不直觉)
printf("%d\n", !!2); // 1 (用 !! 可把任何数值强制变成 0 或 1

The ! operator performs logical negation. If its argument is non-zero, it results in 0. If its argument is 0, it results in 1.

In C there are no bools (not like true/false) but it's an int value. (either 0 or 1) , !x is either 1 or 0, So !!x is a "collapse to 0 or 1 operator" in the sense that any non-zero number is mapped to 1, and 0 stays as it is. This can be useful on occasions.

In C++, !x is a bool type, So !!x is a "collapse to false or true operator" in the sense that any non-zero number is mapped to true, and zero is mapped to false.

!! is not an operator. It’s just the ! operator twice

The first negation converts the data (whatever it data type it may be) to a bool. The second negation changes the boolean again to give the desired result.

双非运算符(!!) 使用两个 ! 运算符。(C++/C#/Javascript)第一次取反操作将操作数转换成与其相反的 bool 值,第二个取反操作将操作数反转回来,这时就成为了明确的 true 和  false 的值。

!!false === false
!!true === true
!!0 === false
!!1 === true
!!-1 === true  // -1 is truthy

!!null === false // null is falsy

What does Double Logical Not Operators (!!) Mean in C

Sometime you will see the following statement in C/C++ source code:

bool isKeepGoing = !!func();

You may wonder, “Why the author uses double logical not (!!)? Convert to the opposite boolean value, and convert back? It doesn’t make any sense to me…”

Well, what the author really wants to do is converting the return value of the func function to a boolean value: The first logical operator will convert whatever return value to the opposite boolean value, and then the second logical operator will convert the opposite value back.

To be honest, this is a horribly obscure way to do the boolean type conversion. Nearly 99.999% of C/C++ programmers won’t understand what the statement really means. They can understand the syntax, but they cannot understand why. “Is there any trick behind this statement?”

Unless you want to obfuscate your source code, please, please don’t use this kind of statement. Instead, you can use normal casting to clearly express what you want to do:

bool isKeepGoing = (bool)func();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值