bool变量。(非标准问题) [英] bool variable.(a non-standard question)

查看:98
本文介绍了bool变量。(非标准问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码说


#define MYBOOL int


此代码非常陈旧。写作的人不可用。我想是什么可能是理由。

1)bool数据类型当时不可用(10年前:纠正我

如果我我错了)

2)int是字对齐的(16位和32位)。上面代码的写者没有
想给编译器做出选择。

如果我们说bool,编译器可以代表他的方式(再次,正确
$ b如果我错的话,请给我一块钱。

这么多我能想到的。


即使在很多书中,我都看到过使用int作为旗帜的作者。例子

Richard Stevens,在unix环境中推进编程 。这个

书使用''restrict''关键字。


现在,我必须将其更改为64bit arch。在我的系统中,性能比其他任何东西更重要。

我想在64arch更改为长。

#define MYBOOL long


对此的任何评论都将不胜感激。

I have code which says

#define MYBOOL int

This code is very old. people who have written is not avaible. I was
thinking what could be reason.
1) bool datatype was not available that time (10 years back: correct me
if i am wrong)
2) int was word aligned (16bit and 32bit). Writter of above code doesnt
want to give choice to compiler.
if we say bool , compiler can represent this on his way (again,correct
me if i am wrong)
This much i can think of it.

even in many books, i have seen author using int as a flag. example
Richard Stevens, "advance programming in unix environment" . and this
book uses ''restrict'' keyword.

Now, i have to change it for 64bit arch. In my system, performance is
more important thn anything else.
I thought of changing it to long on 64arch.
#define MYBOOL long

Any comment on this will be appreciated.

推荐答案

shaanxxx写道:

< snipped。
shaanxxx wrote:
<snipped.

现在,我必须将其更改为64位拱。在我的系统中,性能比其他任何东西更重要。

我想在64arch更改为长。

#define MYBOOL long


如对此有任何意见,我们将不胜感激。
Now, i have to change it for 64bit arch. In my system, performance is
more important thn anything else.
I thought of changing it to long on 64arch.
#define MYBOOL long

Any comment on this will be appreciated.



暂不改变;首先运行并更改它

只有在确实有性能损失的情况下。


-

goose

我冒犯了你吗?发送火焰到root @ localhost

真正的电子邮件:lelanthran在gmail dot com

网站: www.lelanthran.com


shaanxxx写道:
shaanxxx wrote:

我的代码上写着


#define MYBOOL int


这段代码非常陈旧。写作的人不可用。我想是什么可能是理由。

1)bool数据类型当时不可用(10年前:纠正我

如果我我错了)

2)int是字对齐的(16位和32位)。上面代码的写者没有
想给编译器做出选择。

如果我们说bool,编译器可以代表他的方式(再次,正确
$ b如果我错的话,请给我一块钱。

这么多我能想到的。


即使在很多书中,我都看到过使用int作为旗帜的作者。例子

Richard Stevens,在unix环境中推进编程 。这个

书使用''restrict''关键字。


现在,我必须将其更改为64bit arch。在我的系统中,性能比其他任何东西更重要。

我想在64arch更改为长。

#define MYBOOL long
I have code which says

#define MYBOOL int

This code is very old. people who have written is not avaible. I was
thinking what could be reason.
1) bool datatype was not available that time (10 years back: correct me
if i am wrong)
2) int was word aligned (16bit and 32bit). Writter of above code doesnt
want to give choice to compiler.
if we say bool , compiler can represent this on his way (again,correct
me if i am wrong)
This much i can think of it.

even in many books, i have seen author using int as a flag. example
Richard Stevens, "advance programming in unix environment" . and this
book uses ''restrict'' keyword.

Now, i have to change it for 64bit arch. In my system, performance is
more important thn anything else.
I thought of changing it to long on 64arch.
#define MYBOOL long



所以你要声明一个只有
的变量取值0和1而你想要它的类型

是这样的,访问将尽可能快,

那对吗?


假设我做对了然后我不得不说那个

它不在话题中。最终答案取决于你正在使用的处理器,也许还有

操作系统。你的编译器可能知道关于这些东西的b $ b,所以请阅读你的编译器的文档

,看看它是否说明你应如何声明

变量以获得最快的访问权限。

So you want to declare a variable which will only
take the values 0 and 1 and you want its type to
be such that access will be as fast as possible , is
that right ?

Assuming I got it right then I would have to say that
it''s out of topic. Ultimately the answer depends on
the processor you are using and perhaps also the
operating system. Your compiler presumably knows
about these things so read your compiler''s documentation
and see if it says anything on how you should declare
variables in order to get the fastest possible access.


Spiros Bousbouras说:


< snip>
Spiros Bousbouras said:

<snip>

>

所以你要声明一个只有

的变量取值0和1并且你想要它的类型

这样访问将尽可能快,

对吗?
>
So you want to declare a variable which will only
take the values 0 and 1 and you want its type to
be such that access will be as fast as possible , is
that right ?



似乎很可能。当然,除了一个位域(类型为unsigned int和

宽度为1)之外,没有C类型可以/仅/取一个范围内的值

[ 0,1,并且比特场不太可能赢得任何速度奖。


如果速度很高,他应该选择int:


typedef int bool;


但是如果空间非常宝贵,他可以决定他想要多少个bool:


#定义NUM_FLAGS 123 / *或其他* /


然后他可以定义一个unsigned char数组:


#include< limits.h> ;


unsigned char flag [(NUM_FLAGS + CHAR_BIT - 1)/ CHAR_BIT] = {0};


然后使用bit-twiddling宏(例如参见snippets.org)获得

个别位。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

It seems likely. Of course, apart from a bitfield (of type unsigned int and
width 1), there is no C type that can /only/ take a value from the range
[0, 1], and a bitfield would be unlikely to win any awards for speed.

If speed is at a premium, he should choose int:

typedef int bool;

But if space is at a premium, he can decide how many bools he wants:

#define NUM_FLAGS 123 /* or whatever */

Then he can define an array of unsigned char:

#include <limits.h>

unsigned char flag[(NUM_FLAGS + CHAR_BIT - 1) / CHAR_BIT] = {0};

and then use bit-twiddling macros (e.g. see snippets.org) to get at
individual bits.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于bool变量。(非标准问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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