为什么要使用!!(条件),而不是(条件)? [英] Why use !!(condition) instead of (condition)?

查看:158
本文介绍了为什么要使用!!(条件),而不是(条件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过code,人们已经使用条件子句有两个'!的

I've seen code where people have used conditional clauses with two '!'s

#define check_bit(var, pos)       (!!((var) & (1 << (pos))))
#define likely(x)       __builtin_expect(!!(x),1)
#define unlikely(x)     __builtin_expect(!!(x),0)

是一些例子我能找到。

are some of the examples I could find.

有没有使用任何的优势 !!(条件)(条件)

Is there any advantage in using !!(condition) over (condition)?

推荐答案

那么,如果你申请的变量 !! 还不是布尔零个或一个的),那么它会的正常化的价值为 0 1

Well if the variable you are applying !! is not already a bool(either zero or one) then it will normalize the value to either 0 or 1.

对于 __ builtin_expect 此的内核线程的新手讨论的符号和响应的一个解释(重点煤矿的):

With respect to __builtin_expect this kernel newbies thread discusses the notation and one of the responses explains (emphasis mine):

__builtin_expect的签名

The signature of __builtin_expect

http://gcc.gnu.org/onlinedocs/gcc/Other- Builtins.html )是:

长__builtin_expect(长时间曝光,长时间C)

long __builtin_expect (long exp, long c)

注意EXP参数应该是一个不可分割的前pression,因此没有指针
  或浮点类型那里。在否定之否定处理来自转换
  这些类型组成的前pressions自动
。这样一来,您可以简单地
  写:可能(PTR),而不是可能(!PTR = NULL)

Note that the exp parameter should be an integral expression, thus no pointers or floating point types there. The double negation handles the conversion from these types to integral expressions automatically. This way, you can simply write: likely(ptr) instead of likely(ptr != NULL).

有关参考的 C99 布尔宏扩展到 _Bool 真正扩展到 1 扩展到 0 。详细情况在标准草案给出 7.16 布尔类型和值

For reference in C99 bool macro expands to _Bool, true expands to 1 and false expands to 0. The details are given in the draft standard section 7.16 Boolean type and values .

逻辑否定是覆盖在 6.5.3.3 单目算术运算符的段落中的 5 的:

Logical negation is covered in 6.5.3.3 Unary arithmetic operators in paragraph 5:

逻辑否定运算符的结果!是0,如果其操作数的值进行比较
  不等于0,1,如果它的操作数的值进行比较,等于0的结果具有int类型。
  这位前pression!E是等于(0 == E)。

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).

这篇关于为什么要使用!!(条件),而不是(条件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆