`long'保证至少为32位? [英] Is `long` guaranteed to be at least 32 bits?

查看:193
本文介绍了`long'保证至少为32位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过我阅读C ++标准,我一直认为C ++中整数基本类型的大小如下:

  sizeof(char)< = sizeof(short int)< = sizeof(int)< = sizeof(long int)

我从3.9.1 / 2推导出:



  1. 四个有符号整数类型:signed char,short int,
    int和long int。在此列表中,
    每种类型提供至少与
    存储那些位于
    列表中的。 Plain int具有由
    执行环境的架构建议的自然大小


此外, char 的大小由3.9.1 /描述为:

$ b $


$ b
$ b

1.7 / 1更具体地定义了这一点:



  1. 基本存储单元在C ++内存模型中是字节。一个字节至少足够大,可以包含基本执行字符集的任何成员,并由一个连续的位序列组成,其数目由实现定义。


这得出以下结论:

  1 == sizeof(char)< = sizeof(short int)< = sizeof(int)< = sizeof(long int)

其中 sizeof 告诉我们类型是多少字节。此外,实现定义了一个字节中有多少位。我们大多数人可能习惯于处理8位字节,但是标准表示在字节中有 n 位。






this post ,Alf P. Steinbach说:


long(至少)32位。


在我理解根据标准在C ++中基本类型大小的一切时,通常我只会打折这个声明作为一个初学者是错误的,但由于这是阿尔夫我决定这是值得进一步调查。



那么,你说什么?是长期保证的标准是至少32位?如果是这样,请具体说明如何进行此保证。我只是看不到它。


  1. C ++标准专门说,为了知道C ++,你必须知道C / 1) 1


  2. C ++标准隐含地定义值的最小限制a code>可容纳 LONG_MIN - LONG_MAX 2


因此,无论 long 足以将LONG_MIN保持为LONG_MAX。



但是Alf和其他人具体认为长必须至少为32位。这是我想要建立的。 C ++标准是明确的,一个字节中的位数没有指定(它可以是4,8,16,42)所以连接如何能够容纳数字 LONG_MIN-LONG_MAX 到至少32位?






(1)1.2 / 1:文件对于本文件的应用是必不可少的。对于注日期的引用文件,只有引用的版本适用。




  • ISO / IEC 2382(所有部分),信息(包括任何修改)适用于未注明日期的参考文献。技术 - 词汇

  • ISO / IEC 9899:1999,编程语言-C

  • ISO / IEC 10646-1:2000,信息技术 - -Octet编码字符集(UCS) - 第1部分:架构和基本多语言平面



(2)定义在< climits> as:

  LONG_MIN -2147483647 //  - (2 ^ 31  -  1 )
LONG_MAX +2147483647 // 2 ^ 31 - 1


解决方案>

答案是肯定的。阅读我的OP和所有的意见,明白为什么,但这里是短版本。如果你怀疑或质疑任何这一点,我鼓励你阅读整个主题和所有的意见。否则接受为true:


  1. C ++标准包含C标准的一部分,包括 LONG_MIN LONG_MAX

  2. LONG_MIN 大于 -2147483647

  3. LONG_MAX 定义为不小于 +2147483647

  4. 在C ++中,整数类型以二进制形式存储在底层表示中

  5. 用二进制表示 -2147483647 +2147483647 ,需要32位。

  6. A C ++ long保证能够表示 LONG_MIN LONG_MAX
  7. $ b $的最小范围b

因此, long 必须至少为32位 1



EDIT:



LONG_MIN LONG_MAX 的值的大小由C标准(ISO / IEC 9899:TC3)在§5.2.4.2.1节中规定:


[...]它们的实现定义的值应该等于或大于所示的[...](绝对值),具有相同的符号[...]




   - 类型为long的对象的最小值
LONG_MIN -2147483647 // - (2 ^ 31 - 1)
- 类型为long int
的对象的最大值LONG_MAX +2147483647 // 2 ^ 31 - 1






1 32位:这并不意味着 sizeof(long)> = 4 ,因为一个字节不一定是8位。根据标准,一个字节是一些未指定(平台定义)的位数。虽然大多数读者会发现这个奇怪,有真实的硬件上 CHAR_BIT 是16或32。


By my reading of the C++ Standard, I have always understood that the sizes of the integral fundamental types in C++ were as follows:

sizeof(char) <= sizeof(short int) <= sizeof(int) <= sizeof(long int)

I deduced this from 3.9.1/2:

  1. There are four signed integer types: "signed char", "short int", "int", and "long int." In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment

Further, the size of char is described by 3.9.1/ as being:

  1. [...] large enough to store any member of the implementation’s basic character set.

1.7/1 defines this in more concrete terms:

  1. The fundamental storage unit in the C + + memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined.

This leads me to the following conclusion:

1 == sizeof(char) <= sizeof(short int) <= sizeof(int) <= sizeof(long int)

where sizeof tells us how many bytes the type is. Furthermore, it is implementation-defined how many bits are in a byte. Most of us are probably used to dealing with 8-bit bytes, but the Standard says there are n bits in a byte.


In this post, Alf P. Steinbach says:

long is guaranteed (at least) 32 bits.

This flies in the face of everything I understand the size of the fundamental types to be in C++ according to the Standard. Normally I would just discount this statement as a beginner being wrong, but since this was Alf I decided it was worth investigating further.

So, what say you? Is a long guaranteed by the standard to be at least 32 bits? If so, please be specific as to how this guarantee is made. I just don't see it.

  1. The C++ Standard specifically says that in order to know C++ you must know C (1.2/1) 1

  2. The C++ Standard implicitly defines the minimum limit on the values a long can accommodate to be LONG_MIN-LONG_MAX 2

So no matter how big a long is, it has to be big enough to hold LONG_MIN to LONG_MAX.

But Alf and others are specific that a long must be at least 32 bits. This is what I'm trying to establish. The C++ Standard is explicit that the number of bits in a byte are not specified (it could be 4, 8, 16, 42) So how is the connection made from being able to accommodate the numbers LONG_MIN-LONG_MAX to being at least 32 bits?


(1) 1.2/1: The following referenced documents are indispensable for the application of this document. For dated references, only the edition cited applies. For undated references, the latest edition of the referenced document (including any amendments) applies.

  • ISO/IEC 2382 (all parts), Information technology – Vocabulary
  • ISO/IEC 9899:1999, Programming languages – C
  • ISO/IEC 10646-1:2000, Information technology – Universal Multiple-Octet Coded Character Set (UCS) – Part 1: Architecture and Basic Multilingual Plane

(2) Defined in <climits> as:

LONG_MIN -2147483647 // -(2^31 - 1)
LONG_MAX +2147483647 //   2^31 - 1

解决方案

The answer is definitively YES. Read my OP and all the comments to understand why exactly, but here's the short version. If you doubt or question any of this, I encourage you to read the entire thread and all of the comments. Otherwise accept this as true:

  1. The C++ standard includes parts of the C standard, including the definitions for LONG_MIN and LONG_MAX
  2. LONG_MIN is defined as no greater than -2147483647
  3. LONG_MAX is defined as no less than +2147483647
  4. In C++ integral types are stored in binary in the underlying representation
  5. In order to represent -2147483647 and +2147483647 in binary, you need 32 bits.
  6. A C++ long is guaranteed to be able to represent the minimum range LONG_MIN through LONG_MAX

Therefore a long must be at least 32 bits1.

EDIT:

LONG_MIN and LONG_MAX have values with magnitudes dictated by the C standard (ISO/IEC 9899:TC3) in section §5.2.4.2.1:

[...] Their implementation-defined values shall be equal or greater in magnitude [...] (absolute value) to those shown, with the same sign [...]

— minimum value for an object of type long int
LONG_MIN -2147483647 // -(2 ^ 31 - 1)
— maximum value for an object of type long int
LONG_MAX +2147483647 // 2 ^ 31 - 1


1 32 bits: This does not mean that sizeof (long) >= 4, because a byte is not necessarily 8 bits. According to the Standard, a byte is some unspecified (platform-defined) number of bits. While most readers will find this odd, there is real hardware on which CHAR_BIT is 16 or 32.

这篇关于`long'保证至少为32位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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