布尔值的排序 [英] Ordering of boolean values
问题描述
在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(bprintf(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中的
编辑(由James提供)除非另有说明,否则此问题的不等式是正确的。上面的编辑不是真的正确,在stdbool.h
也强制布尔值为0(false)或1(true) -bool b = -1;
将b
的值设置为1.由于1 < 1
和1< 0
至少为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),
bool
s compare exactly as iffalse
were0
andtrue
were1
. And if the type isbool
, no values other thantrue
andfalse
are possible.When comparing
bool
to other numeric types, it will convert toint
, again withfalse
converting to0
andtrue
converting to1
.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 ofb
to 1. Since1 < 1
and1 < 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 offalse
ortrue
. It's only when it is promoted toint
that the conversion creates the values of0
and1
.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 meansbool
converts toint
(as doeschar
orshort
... or an enum).All of which is rather technical. In practice, you can remember that
false
<true
, or you can considerfalse
is 0 andtrue
is 1, whichever works best for you. The only important thing to remember is that abool
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 patterns0x55
and0xAA
, for example, as long as all conversions to an integral type gave 0 and 1, conversion tobool
always gave the appropriate value, etc. Including zero initialization of static variables.)And one final note:
bool b = -1;
setsb
to-1 != 0
(which istrue
, not1
, but of course,true
will convert to1
in any numeric context.这篇关于布尔值的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!