布尔值排序 [英] Ordering of boolean values
问题描述
在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), bool
s 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屋!