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

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

问题描述

在C ++或< stdbool.h> 从C99,怎么是小于运算符< 布尔值定义?

另外,解释code的行为:

 的#ifndef __cplusplus
#包括LT&;&stdbool.h GT;
#万一
#包括LT&;&stdio.h中GT;诠释主(){
    布尔B = -1;
    如果(B<真){
        的printf(B<真\\ n);
    }
    如果(B<假){
        的printf(B<假\\ n);
    }
    如果(真<假){
        的printf(真<假\\ n);
    }
    如果(假lt;真){
        的printf(假<真\\ n);
    }
}

在MSVC版本10,编译为C ++ code,GCC 4.6.3,ubuntu5编译为C code和G ++编译4.6.3-1ubuntu5像C ++ code,你得到的是

 假LT;真正

这就是下面的不等式均

 (布尔)-1 LT;真正
(布尔)-1 LT;假
真<假

和以下是真正

 假LT;真正


解决方案

在C ++(我用C怀疑为好),布尔如果正好比较,如果
0 真正 1 。如果类型是布尔,无
其他值真正是可能的。

当比较布尔来的数字类型,它会转换为 INT
再次与转换为 0 真正转换为 1

编辑: C ++和 stdbool.h 在C99也迫使布尔值是0(假)或1(真) - 布尔b = -1; 的值设为1。由于 1 LT; 1 1 LT; 0 都是假的,在这个问题的不平等是正确的。

编辑:(詹姆斯)除上述修改是不是真的正确,在
至少对于C ++。 A 布尔不具有值为0或1,它有一个价值
真正。只有当它被提升到 INT
转换创建的值 0 1

和作为康拉德指出,没有布尔值测试结果的比​​较。
通常的算术转换对出现的比较运营商,
这意味着在两个操作数的整体推广,这意味着
布尔转换成 INT (一样字符 ...或枚举)。

所有这一切是相当的技术。在实践中,你可以记住
< 真正,也可以考虑 0和真正是1,
哪个最适合你。需要记住的唯一重要的是
一个布尔可以拥有的没有的其他值。

(有趣的是,我不认为一个布尔是位模式
该标准规定。一个实现可以使用这些位模式
将0x55 和0xAA ,例如,只要所有转换的
整体式给了0和1,转换为布尔总是给出
合适的值,等包括零初始化静态
变量。)

和最后一个音符:布尔B = -1; B - 1!= 0 (这是
真正,而不是 1 ,当然,真正将转换为 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天全站免登陆