bool细节 [英] bool specifics

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

问题描述

以下打印不等于在所有实施中:


#include< iostream>


签署main()

{

bool k;


k = 5;


std :: cout<< (k == 5?等于:不等于);

}

让我们说一个bool是特定的8位实现。当你将
分配给bool时,bool的位模式是否代表:


A)整数1

B)整数5

C)实施定义?

以下陈述是否始终相同:


k = true;

++ k;


k = false;

--k;

即使k最初被设置为5?

-JKop

Will the following print "NOT equal" on all implementations:

#include <iostream>

signed main()
{
bool k;

k = 5;

std::cout << (k == 5 ? "equal" : "NOT equal");
}
Let''s say that a bool is 8-Bit on a particular implementation. When you
assign 5 to a bool, does the bool''s bit pattern represent:

A) The integer 1

B) The integer 5

C) Implementation-defined ?
Are the following statments equivalent at all times:

k = true;
++k;

k = false;
--k;
Even if k had initially been set to 5?
-JKop

推荐答案

请问以下打印


不等于

true

true


在所有实施中?

#include< iostream>


签名main()

{

bool k;


k = 5;


std :: cout<< (k == 5?相等:不等于)<< ''\ n'';


k - = 2;


std :: cout<< (k?" true":" false")<< ''\ n'';


k - = 3;


std :: cout<< (k?" true":" false");

}

k - = 2的影响是什么?

- JKop
Will the following print

NOT equal
true
true

on all implementations?
#include <iostream>

signed main()
{
bool k;

k = 5;

std::cout << (k == 5 ? "equal" : "NOT equal") << ''\n'';

k -= 2;

std::cout << ( k ? "true" : "false") << ''\n'';

k -= 3;

std::cout << ( k ? "true" : "false");
}
What is the effect of k -=2 ?
-JKop


JKop写道:
JKop wrote:
以下打印不等于在所有实施中:

#include< iostream>

签署main()
{
bool k;

k = 5;

std :: cout<< (k == 5?等于:不等于);
}

假设bool在特定实现上是8位。当你为布尔分配5时,布尔的位模式是否代表:

A)整数1


也许。

B)整数5


也许。

C)实现定义?


是。

以下陈述是否始终相同:

k = true;
++ k ;

k = false;
- k;


它们永远不等同。第一个将k设置为false,第二个将
设置为true。

即使k最初设置为5?
Will the following print "NOT equal" on all implementations:

#include <iostream>

signed main()
{
bool k;

k = 5;

std::cout << (k == 5 ? "equal" : "NOT equal");
}
Let''s say that a bool is 8-Bit on a particular implementation. When
you assign 5 to a bool, does the bool''s bit pattern represent:

A) The integer 1
Maybe.
B) The integer 5
Maybe.
C) Implementation-defined ?
Yes.
Are the following statments equivalent at all times:

k = true;
++k;

k = false;
--k;
They are never equivalent. The first one sets k to false, the second one
sets it to true.
Even if k had initially been set to 5?




bool只知道两个值,true和false。如果为它分配一个整数

,那么该整数首先被转换为bool(0导致

为真,其他任何值为false),然后k被设置这个价值。



A bool only knows two values, true and false. If you assign an integer
to it, that integer gets converted to bool first (with 0 resulting in
true, any other value in false), and then k gets set to that value.




" JKop" < NU ** @ NULL.NULL>在留言新闻中写道:Ps ****************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message news:Ps******************@news.indigo.ie...
bool k;

k = 5;


这将k设置为true

std :: cout<< (k == 5?等于:不等于);


这里将k的值提升为int。如果k之前是真的那么它的值将是1



让我们说一个bool在特定实现上是8位。当你为bool分配5时,bool的位模式是否代表:\
bool k;

k = 5;
This sets k to "true"
std::cout << (k == 5 ? "equal" : "NOT equal");
The value of k is promoted to int here. It''s value will be 1
if k had been true before.

Let''s say that a bool is 8-Bit on a particular implementation. When you
assign 5 to a bool, does the bool''s bit pattern represent:\




它代表一个真实的值。这取决于实现如何对它进行编码。

。然而。所有具有真值的bool都比较相等。

所有bool true值在转换为整数时转换为1.


所以,尽管它完全可以接受实现在bool中存储一个表示

的5,你很难分辨,因为

static_cast< int>(k)

将始终为1.



It represents a value that would be true. It''s up to the implementation to figure
out how to encode it. However. All bools with true values compare equal.
All bool true values convert to 1 when converted to an integer.

So, while it is perfectly acceptable for the implementation to store a representation
of 5 in the bool, it will be hard for you to tell, as
static_cast<int>(k)
will always be 1.


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

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