整数的大小 [英] Sizes of integers

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

问题描述



我想知道是否有任何理由让不同的系统有不同尺寸的短,整数和长期?$ / $
我认为这使得计划用于你的程序的类型变得更加困难。

例如,如果我想制作一个使用的便携式程序,

说,一个计数器的int类型,一个系统使用16位的int,而另一个系统使用32位,然后,为了让我的程序运行

在所有系统上我都需要使用相同的最小值,对吧?

在这种情况下,我最多只能计算16位,否则就会产生
在使用16位整数的系统上溢出。


所以我不明白为什么要有不同的大小。如果您希望您的

程序可以移植并在所有系统上运行相同的程序,我们该如何做?
那个?只使用整数类型的最小保证大小?


我认为stdint.h解决了这个问题,从那以后你知道你有多大的尺寸

你的类型,但那是在C99。


/ Michael

Hi,
I wonder if there is any good reason to let different systems have
different sizes of short, int and long?
I think that makes it harder to plan which type to use for your program.
For example, if I want to make a portable program which uses,
say, an int type for a counter, and one system uses 16-bit for ints, and
some other system uses 32-bits, then, in order to have my program to run
the same way on all systems I need to use the smallest value, right?
In this case, I could only count up to 16-bits, since otherwise it would
overflow on the system that uses 16-bit ints.

So I don''t understand why to have different sizes. If you want your
programs to be portable and run the same on all systems, how do we do
that? By only using the minimum guaranteed size of the integer types?

I think stdint.h solves that, since then you know which size you have on
your type, but thats in C99.

/Michael

推荐答案

Michael Brennan< br *** *********@gmail.com>写道:
Michael Brennan <br************@gmail.com> writes:

我想知道是否有任何合理的理由让不同的系统具有不同大小的short,int和long?
我认为这使得计划使用哪种类型的程序变得更加困难。
例如,如果我想制作一个便携式程序,使用,例如,一个计数器的int类型,以及一个系统使用16位进行整数,而其他一些系统使用32位,然后,为了让我的程序在所有系统上以相同的方式运行,我需要使用最小的值,对吗?
在这种情况下,我只能计数16位,否则它会在使用16位整数的系统上溢出。
所以我不明白为什么要有不同的尺码。如果您希望您的
程序可移植并在所有系统上运行相同,我们该怎么办呢?仅使用整数类型的最小保证大小?
我认为stdint.h解决了这个问题,从那以后你就知道你的类型有多大,但是在C99中就是这样。
Hi,
I wonder if there is any good reason to let different systems have
different sizes of short, int and long?
I think that makes it harder to plan which type to use for your program.
For example, if I want to make a portable program which uses,
say, an int type for a counter, and one system uses 16-bit for ints, and
some other system uses 32-bits, then, in order to have my program to run
the same way on all systems I need to use the smallest value, right?
In this case, I could only count up to 16-bits, since otherwise it would
overflow on the system that uses 16-bit ints. So I don''t understand why to have different sizes. If you want your
programs to be portable and run the same on all systems, how do we do
that? By only using the minimum guaranteed size of the integer types? I think stdint.h solves that, since then you know which size you have on
your type, but thats in C99.



便携性,以及你的决定,并不是来自某个特定平台的整数大小,而是来自你希望那些值为b $ b整数变量的值。


如果你的计数器只能保存16位

整数的整数,那么使用int16_t。


如果你的计数器可以*溢出一个16位整数,那么你需要在所有平台上使用

a更长,比如32位整数 - 包括任何平台

只有16位原生整数。


如果在任何平台上存在任何危险,不要随意使用''int'。


-

Chris。


The portability, and your decision, doesn''t come from the size of a
particular platform''s integers, but from the values you wish those
integer variables to hold.

If your counter will only hold integers that may be held in 16-bit
integers, then use int16_t.

If your counter can *ever* overflow a 16-bit integer, then you require
a longer, say 32-bit, integer on all platforms - including any platforms
that offer only 16-bit native integers.

Don''t casually use ''int''s if there''s ever a danger, on any platform.

--
Chris.


Chris McDonald写道:
Chris McDonald wrote:
Michael Brennan< br ************@gmail.com>写道:

Michael Brennan <br************@gmail.com> writes:

我想知道是否有任何理由让不同的系统有不同大小的short,int和long?
我认为这会让你更难规划用于你的程序的类型。
例如,如果我想制作一个便携式程序,它使用一个int类型的计数器,一个系统使用16位进行整数,而其他一些系统使用32位,然后,为了让我的程序在所有系统上以相同的方式运行,我需要使用最小的值,对吧?
在这种情况下,我只计算16位,否则它会在使用16位整数的系统上溢出。
Hi,
I wonder if there is any good reason to let different systems have
different sizes of short, int and long?
I think that makes it harder to plan which type to use for your program.
For example, if I want to make a portable program which uses,
say, an int type for a counter, and one system uses 16-bit for ints, and
some other system uses 32-bits, then, in order to have my program to run
the same way on all systems I need to use the smallest value, right?
In this case, I could only count up to 16-bits, since otherwise it would
overflow on the system that uses 16-bit ints.


< br>



所以我不明白为什么要有不同的尺寸。如果您希望您的
程序可移植并在所有系统上运行相同,我们该怎么办呢?只使用整数类型的最小保证大小?
So I don''t understand why to have different sizes. If you want your
programs to be portable and run the same on all systems, how do we do
that? By only using the minimum guaranteed size of the integer types?





我认为stdint.h解决了这个问题,从那以后你知道你有哪个大小<你的类型,但那是在C99。
I think stdint.h solves that, since then you know which size you have on
your type, but thats in C99.



可移植性,你的决定,不是来自特定平台的整数大小,但是从你希望那些整数变量保持的值。

如果你的计数器只保存可以用16位整数保存的整数,那么使用int16_t。



The portability, and your decision, doesn''t come from the size of a
particular platform''s integers, but from the values you wish those
integer variables to hold.

If your counter will only hold integers that may be held in 16-bit
integers, then use int16_t.



如果您的目标是C99,这可能是一个使用

int_fast16_t之类的好地方。即使对于C90,也许值得定义这些类型的

自己的版本。


-

Ian Collins。


If you are targeting C99, this is probably a good place to use
int_fast16_t and the like. Even for C90, it may be worth defining your
own versions of these types.

--
Ian Collins.


在文章< Ev ******************* @ newsb.telia.net> ;,

Michael Brennan< br ************ @ gmail.com>写道:
In article <Ev*******************@newsb.telia.net>,
Michael Brennan <br************@gmail.com> wrote:
我想知道是否有任何理由让不同的系统有不同大小的short,int和long?
我认为这样会更难规划哪种类型用于您的程序。
所以我不明白为什么要有不同的尺码。如果您希望您的
程序可移植并在所有系统上运行相同,我们该怎么办呢?只使用整数类型的最小保证大小?
I wonder if there is any good reason to let different systems have
different sizes of short, int and long?
I think that makes it harder to plan which type to use for your program. So I don''t understand why to have different sizes. If you want your
programs to be portable and run the same on all systems, how do we do
that? By only using the minimum guaranteed size of the integer types?




三个理由:


1)当时有系统没有使用8位的倍数

作为原始大小。曾经有一段时间它看起来像36位

的话会赢得胜利。


2)性能。必须在早期的

系统上合成32位算术。


3)根据一些DSP和嵌入式系统的人们

在新闻组中,这些天有很多系统只需提供非常有限的存储空间(例如,只有32位)。

这些处理器用于的应用程序,

其他尺寸的使用频率不足以使它值得占用它们的模具空间。模具空间越小,设备上的时钟就越快......

-

当时我还很年轻,但是我也很朦胧。

- 克里斯托弗牧师



Three reasons:

1) Back then, there were systems that didn''t use multiples of 8 bits
as their native sizes. There was a time when it looked like 36 bit
words were going to win out.

2) Performance. 32 bit arithmetic had to be synthesized on earlier
systems.

3) According to some of the DSP and embedded systems people
in the newsgroup, there are a bunch of systems these days which
only offer a very limited number of storage sizes (e.g., only 32 bit).
For the kinds of applications those processors are intended for,
the other sizes are not used often enough to make it worth taking up
the die space for them. The less die space, the faster you can clock
the device...
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest


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

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