C99条件返回值

标签 c optimization standards c99

我正在尝试优化一些代码,我想知道是否像

这样的条件的返回值
(1>0)

在 c99 中总是 1 吗?我在网上找不到答案,我用 gcc 做的一些测试似乎表明这是真的。但这是语言规范的一部分吗?

具体代码(步进方 block 算法):

to_run->data[(y / 2) * (my_grid->width / 2) + (x / 2)] =
    (up[0] > level) +
    (up[1] > level) << 1 +
    (down[0] > level) << 2 +
    (down[1] > level) << 3;

最佳答案

是的。

C99 §6.5.8 Relational operators

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int

关于C99条件返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20119415/

相关文章:

c - ld : 0711-317 error : Undefined symbol: . kget_proc_info

c - 仅使用 "~ & ^ | + << >> ="个运算符和 20 个运算实现逻辑右移

c - 可变大小的对象可能未被初始化

opencv - 这种时差的原因是什么?

C 数组 = 比 memcpy() 更快的数组

c - 将宏扩展为无代码 : "do {} while (0)" or "while (0) {}"

mysql - 为什么 GROUP BY on FULLTEXT INDEX 使用临时的?

c++ - uninitialized_copy/fill(In first, In last, For dest, A &a) 是 c++ 标准中的疏忽吗?

php - 使用分离层与数据库交互(PHP 和 WordPress)

laravel - 单元测试、集成测试还是功能测试?