布尔值的排序 [英] Ordering of boolean values

查看:184
本文介绍了布尔值的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C99下的C ++或< stdbool.h> 下,如何使用less-than运算符<

 <$ c 

$ c> #ifndef __cplusplus
#include< stdbool.h>
#endif
#include< stdio.h>

int main(){
bool b = -1;
if(b< true){
printf(b< true\\\
);
}
if(b printf(b );
}
if(true< false){
printf(true< false\\\
);
}
if(false< true){
printf(false< true\\\
);在MSVC版本10下,编译为C ++代码,GCC(编译为C ++代码) 4.6.3-ubuntu5编译为C代码,G ++ 4.6.3-1ubuntu5编译为C ++代码,你所得到的是

  false < true 

也就是说,以下不等式均为 false

 (bool)-1< true 
(bool)-1< false
true< false

并且以下是 true

  false<真正的


解决方案

), bool 的比较与
false 的比较 0 true 都是 1 。如果类型为 bool ,则不会有
值而不是 true false



bool 与其他数字类型进行比较时,会转换为 int
再次 false 转换为 0 true 转换为 1



C99中的 stdbool.h 也强制布尔值为0(false)或1(true) - bool b = -1; b 的值设置为1.由于 1 < 1 1< 0

编辑(由James提供)除非另有说明,否则此问题的不等式是正确的。上面的编辑不是真的正确,在
至少为C ++。 bool 没有值0或1,它具有
的值 false 真实。只有当它被提升为 int 时,
转换创建 0 1



正如Konrad所指出的,没有比较 bool 值。
对于比较运算符
进行通常的算术转换,这意味着在两个操作数上的积分提升,这意味着
bool 转换为 int char short 。或枚举)。



这些都是相当技术性的。在实践中,你可以记住
false < true ,或者您可以考虑 false 为0和 true 是1,
哪一个最适合你。唯一要记住的重要事情是
bool 可以有其他值



(有趣的是,我不认为 bool 的位模式是由标准强加的
。实现可以使用位模式例如,
0x55 0xAA ,只要所有转换为
整数类型给出0和1,转换为 bool 总是给出
适当的值等。包括静态
变量的零初始化。)



最后一个注意事项: bool b = -1; 设置 b -1!= 0 (这是
true ,而不是 code>,但当然, true 将在任何
数值上下文中转换为 1


Under C++ or <stdbool.h> from C99, how is the less-than operator < defined for boolean values?

Alternatively, explain the behaviour of this code:

#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <stdio.h>

int main() {
    bool b = -1;
    if(b < true) {
        printf("b < true\n");
    }
    if(b < false) {
        printf("b < false\n");
    }
    if(true < false) {
        printf("true < false\n");
    }
    if(false < true) {
        printf("false < true\n");
    }
}

Under MSVC version 10, compiled as C++ code, GCC 4.6.3-ubuntu5 compiled as C code and G++ 4.6.3-1ubuntu5 compiled as C++ code, all you get is

false < true

That is, the following inequalities are all false:

(bool)-1 < true
(bool)-1 < false
true < false

And the following is true:

false < true

解决方案

In C++ (and I suspect in C as well), bools compare exactly as if false were 0 and true were 1. And if the type is bool, no values other than true and false are possible.

When comparing bool to other numeric types, it will convert to int, again with false converting to 0 and true converting to 1.

Edit: Both C++ and stdbool.h in C99 also force boolean values to be either 0 (false) or 1 (true) - bool b = -1; sets the value of b to 1. Since 1 < 1 and 1 < 0 are both false, the inequalities in the question are correct.

Edit: (by James) Except that the above edit isn't really correct, at least for C++. A bool doesn't have a value of 0 or 1, it has a value of false or true. It's only when it is promoted to int that the conversion creates the values of 0 and 1.

And as Konrad has pointed out, there is no conparison of bool values. The "usual arithmetic conversions" occur for the comparison operators, which means integral promotion on both of the operands, which means bool converts to int (as does char or short... or an enum).

All of which is rather technical. In practice, you can remember that false < true, or you can consider false is 0 and true is 1, whichever works best for you. The only important thing to remember is that a bool can have no other values.

(Interestingly, I don't think that the bit patterns of a bool are imposed by the standard. An implementation could use the bit patterns 0x55 and 0xAA, for example, as long as all conversions to an integral type gave 0 and 1, conversion to bool always gave the appropriate value, etc. Including zero initialization of static variables.)

And one final note: bool b = -1; sets b to -1 != 0 (which is true, not 1, but of course, true will convert to 1 in any numeric context.

这篇关于布尔值的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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