整数大小 [英] Integer sizes

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

问题描述

最常见的整数类型大小似乎是:


8位 - 签名字符

16位 - 短

16或32位 - int

32或64位 - 长


问题#1:


有人知道除了这些以外常见的尺寸吗?我知道

标准表示字节中的位数。 (char)是> = 8,但我还没有

遇到除了这些之外的其他值...


问题#2:


是否有人知道包含一些便携式定义的东西

如Int8,Int16和Int32(假设它们可用于平台)?

如果没有,我认为你可以使用< limits.h>

(< climits>?)中的定义来做到这一点。我知道预处理器并不理解

sizeof运算符,所以你不能这样做。


谢谢,


David F

The most common sizes of integer types seem to be:

8 bits - signed char
16 bits - short
16 or 32 bits - int
32 or 64 bits - long

Question #1:

Does anyone know how common sizes other than these are ? I know that the
standard says that number of bits in a "byte" (char) is >= 8, but I have not
come across values other than these yet ...

Question #2:

Is anyone aware of a package that has some portable definitions of things
like Int8, Int16 and Int32 (assuming they are possible for the platform) ?
If not, I think you could do this using the definitions in <limits.h>
(<climits> ?). I am aware that the preprocessor doesn''t understand the
sizeof operator, so you can''t do it that way.

Thanks,

David F

推荐答案

2003年12月4日星期四16:10:07 +1100,大卫Fisher"

< no **** @ nospam.nospam.nospam>在comp.lang.c ++中写道:
On Thu, 4 Dec 2003 16:10:07 +1100, "David Fisher"
<no****@nospam.nospam.nospam> wrote in comp.lang.c++:
整数类型的最常见大小似乎是:

8位 - 签名字符
16位 - 简短
16或32位 - int
32或64位 - 长

问题#1:

有谁知道除了常见的尺寸以外这些是 ?我知道
标准表示字节中的位数。 (char)是> = 8,但我还没有看到除了这些之外的其他值......


一般不适用于普通处理器,从8位开始高达64位或
,但这对于DSP(数字信号处理器)来说非常普遍。

现在我正在为德州仪器的DSP编写一个在小于16位字的任何内容中都不能访问
内存。 CHAR_BIT在那个

平台上是16。


有些DSP只处理32位数量,所以

字符类型,短裤,整数和长音都是32位。


摩托罗拉56K DSP系列的早期成员有一个24位字大小的
。 IIRC,char,short和int都是24位,

长是48位。

问题2:

是否有人知道一个包含一些可移植的东西定义的包,比如Int8,Int16和Int32(假设它们可以用于平台)?
如果没有,我认为你可以使用< limits中的定义来做到这一点。 h>
(< climits>?)。我知道预处理器并不理解
sizeof运算符,所以你不能这样做。

谢谢,

David F
The most common sizes of integer types seem to be:

8 bits - signed char
16 bits - short
16 or 32 bits - int
32 or 64 bits - long

Question #1:

Does anyone know how common sizes other than these are ? I know that the
standard says that number of bits in a "byte" (char) is >= 8, but I have not
come across values other than these yet ...
Generally not for ordinary processors, from 8-bit on up to 64-bit or
so, but this is quite common for DSPs (Digital Signal Processors).
Right now I''m coding for a Texas Instruments DSP that doesn''t access
memory in anything smaller than 16 bit words. CHAR_BIT is 16 on that
platform.

There are some DSPs that only deal with 32 bit quantities, so the
character types, shorts, ints, and longs are all 32 bits.

There were some early members of the Motorola 56K DSP family with
24-bit word size. IIRC, char, short and int were all 24 bits, and
long was 48 bits.
Question #2:

Is anyone aware of a package that has some portable definitions of things
like Int8, Int16 and Int32 (assuming they are possible for the platform) ?
If not, I think you could do this using the definitions in <limits.h>
(<climits> ?). I am aware that the preprocessor doesn''t understand the
sizeof operator, so you can''t do it that way.

Thanks,

David F




当前的1999版C标准包括一个名为

< stdint.h>的标题。包含各种整数类型的typedef。

所需类型包括:int_least#_t,uint_least#_t,int_fastest#_t

和uint_fastest#_t,其中#至少包含8,16,32和64.


(u)int_least#_t是整数类型的typedef,占用至少包含这么多位的内存最少量的内存。 />

(u)int_fastest#_t是整数类型的typedef,包含至少
,这是处理器使用最快的那些位,如果

有多个类型至少包含这个位数。


在C99下某些情况下可选的类型是:


(u)int8_t,(u)int16_t,(u)int32_t和(u)int64_t。


这些是可选的,因为某些实现可能实际上不是

具有恰好宽度的硬件类型。如果某个平台实际上具有完全类型且具有这些位宽的整数类型,如果这些类型具有

而不是填充位,并且如果签名版本使用2'的补码

表示,然后实现必须提供typedef。


但这必须是针对编译器定制的,即typedef必须

设置为适当的本机类型。当然,对于特定的编译器,你只能执行一次

,然后你可以永远包含并使用

标题。


我建议使用这些类型,因为它们可以在C

编译器中移植,并且几乎肯定包含在下一版本的C ++

标准中。由于几乎所有的C ++编译器都包含一个C编译器,因此很多C ++编译器都带有这个头文件。对于64位类型可能的例外情况,即C签名和无符号

long long,C ++实现也应该能够使用它们。


这是一个< stdint.h>的样本。关于英特尔x86处理器的每个C和C ++编译器,旧的16位b $ b位编译器以及更新的32位编译器(可能的<),标题应该只用

br />
64位类型例外)。


#ifndef STDINT_H_INCLUDED

#define STDINT_H_INCLUDED

typedef signed char int8_t;

typedef unsigned char uint8_t;

typedef signed short int16_t;

typedef unsigned short uint16_t;

typedef signed long int32_t;

typedef unsigned long uint32_t;

typedef signed long long int64_t;

typedef unsigned long long uint64_t;


typedef signed char int_least8_t;

typedef unsigned char uint_least8_t;

typedef signed short int_least16_t;

typedef unsigned short uint_least16_t;

typedef signed long int_least32_t;

typedef unsigned long uint_least32_t;

typedef signed long long in t_least64_t;

typedef unsigned long long uint_least64_t;


typedef signed char int_fast8_t;

typedef unsigned char uint_fast8_t;

typedef签名短int_fast16_t;

typedef unsigned short uint_fast16_t;

typedef signed long int_fast32_t;

typedef unsigned long uint_fast32_t;

typedef签名long long int_fast64_t;

typedef unsigned long long uint_fast64_t;


#define INT8_MAX 127

#define INT16_MAX 32767

#define INT32_MAX 2147483647

#define INT64_MAX 9223372036854775807LL


#define INT8_MIN -128

#define INT16_MIN(-INT16_MAX - 1)

#define INT32_MIN(-INT32_MAX - 1)

#define INT64_MIN(-INT64_MAX - 1)


#define UINT8_MAX 255

#define UINT16_MAX 65535

#define UINT32_MAX 4294967295

#define UINT64_MAX 18446744073709551615


#define INT_LEAST8_MAX 127

#define INT_LEAST16_MAX 32767

#define INT_LEAST32_MAX 2147483647

#define INT_LEAST64_MAX 9223372036854775807


#define INT_LEAST8_MIN -128

#define INT_LEAST16_MIN(-INT_LEAST16_MAX - 1)

#define INT_LEAST32_MIN(-INT_LEAST32_MAX - 1)

#define INT_LEAST64_MIN (-INT_LEAST64_MAX - 1)


#define UINT_LEAST8_MAX 255

#define UINT_LEAST16_MAX 65535

#define UINT_LEAST32_MAX 4294967295

#define UINT_LEAST64_MAX 18446744073709551615

#define INT_FAST8_MAX 127

#define INT_FAST16_MAX 32767

#define INT_FAST32_MAX 2147483647

#define INT_FAST64_MAX 9223372036854775807

#define INT_FAST8_MIN -128)

#define INT_FAST16_MIN(-INT_FAST16_MAX - 1)

#define INT_FAST32_MIN(-INT_FAST32_MAX - 1)

#define INT_FAST64_MIN(-INT_FAST64_MAX - 1)


#define UINT_FAST8_MAX 255

#define UINT_FAST16_MAX 65535

#define UINT_FAST32_MAX 4294967295

#define UINT_FAST64_MAX 18446744073709551615

#endif


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top。 html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++ ftp://snurse-l.org/pub/acllc-c++/faq



The current 1999 version of the C standard includes a header named
<stdint.h> that contains typedefs for a variety of integer types. The
required types include: int_least#_t, uint_least#_t, int_fastest#_t
and uint_fastest#_t, where # consists of at least 8, 16, 32, and 64.

The (u)int_least#_t are typedefs for the integer types occupying the
least amount of memory containing at least that many bits.

The (u)int_fastest#_t are typedefs for the integer types containing at
least that many bits that are the fastest for the processor to use, if
there is more than one type with at least that number of bits.

The types that are optional under some circumstances under C99 are:

(u)int8_t, (u)int16_t, (u)int32_t, and (u)int64_t.

These are optional because some implementations might not actually
have hardware types with exactly those widths. If a platform actually
has integer types with exactly those bit widths, if those types have
not padding bits, and if the signed versions use 2''s complement
representation, then the implementation must provide the typedefs.

But this must be tailored to the compiler, that is the typedefs must
be set to the appropriate native type. Of course you only do that
once for a particular compiler, then you can include and use the
header forever.

I would suggest using these types, as they will be portable across C
compilers and almost certainly included in the next version of the C++
standard. Since almost all C++ compilers also include a C compiler,
many C++ compilers come with this header already. With the possible
exception of the 64 bit types, which is the C signed and unsigned
"long long", C++ implementations should be able to use them as well.

Here''s a sample of a <stdint.h> header that should work with just
about every C and C++ compiler for the Intel x86 processors, older 16
bit compilers as well as newer 32 bit ones (with the possible
exception of the 64 bit types).

#ifndef STDINT_H_INCLUDED
#define STDINT_H_INCLUDED
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed long int32_t;
typedef unsigned long uint32_t;
typedef signed long long int64_t;
typedef unsigned long long uint64_t;

typedef signed char int_least8_t;
typedef unsigned char uint_least8_t;
typedef signed short int_least16_t;
typedef unsigned short uint_least16_t;
typedef signed long int_least32_t;
typedef unsigned long uint_least32_t;
typedef signed long long int_least64_t;
typedef unsigned long long uint_least64_t;

typedef signed char int_fast8_t;
typedef unsigned char uint_fast8_t;
typedef signed short int_fast16_t;
typedef unsigned short uint_fast16_t;
typedef signed long int_fast32_t;
typedef unsigned long uint_fast32_t;
typedef signed long long int_fast64_t;
typedef unsigned long long uint_fast64_t;

#define INT8_MAX 127
#define INT16_MAX 32767
#define INT32_MAX 2147483647
#define INT64_MAX 9223372036854775807LL

#define INT8_MIN -128
#define INT16_MIN (-INT16_MAX - 1)
#define INT32_MIN (-INT32_MAX - 1)
#define INT64_MIN (-INT64_MAX - 1)

#define UINT8_MAX 255
#define UINT16_MAX 65535
#define UINT32_MAX 4294967295
#define UINT64_MAX 18446744073709551615

#define INT_LEAST8_MAX 127
#define INT_LEAST16_MAX 32767
#define INT_LEAST32_MAX 2147483647
#define INT_LEAST64_MAX 9223372036854775807

#define INT_LEAST8_MIN -128
#define INT_LEAST16_MIN (-INT_LEAST16_MAX - 1)
#define INT_LEAST32_MIN (-INT_LEAST32_MAX - 1)
#define INT_LEAST64_MIN (-INT_LEAST64_MAX - 1)

#define UINT_LEAST8_MAX 255
#define UINT_LEAST16_MAX 65535
#define UINT_LEAST32_MAX 4294967295
#define UINT_LEAST64_MAX 18446744073709551615

#define INT_FAST8_MAX 127
#define INT_FAST16_MAX 32767
#define INT_FAST32_MAX 2147483647
#define INT_FAST64_MAX 9223372036854775807

#define INT_FAST8_MIN -128)
#define INT_FAST16_MIN (-INT_FAST16_MAX - 1)
#define INT_FAST32_MIN (-INT_FAST32_MAX - 1)
#define INT_FAST64_MIN (-INT_FAST64_MAX - 1)

#define UINT_FAST8_MAX 255
#define UINT_FAST16_MAX 65535
#define UINT_FAST32_MAX 4294967295
#define UINT_FAST64_MAX 18446744073709551615
#endif

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


2003年12月4日星期四16:10:07 +1100,David Fisher

< no **** @ nospam.nospam.nospam> ;写道:
On Thu, 4 Dec 2003 16:10:07 +1100, "David Fisher"
<no****@nospam.nospam.nospam> wrote:
整数类型最常见的大小似乎是:

8位 - 签名字符
16位 - 简短
16或32位 - int
32或64位 - 长

问题#1:

有谁知道除了这些之外的常见尺寸是多少?我知道
标准表示字节中的位数。 (char)是> = 8,但我还没有看到除了这些之外的其他价值......

问题#2:

有谁知道一个包含一些容易定义的东西的包,比如Int8,Int16和Int32(假设它们可能用于平台)?
如果没有,我认为你可以使用< limits中的定义来做到这一点.h>
(< climits>?)。我知道预处理器并不理解
sizeof运算符,所以你不能这样做。
The most common sizes of integer types seem to be:

8 bits - signed char
16 bits - short
16 or 32 bits - int
32 or 64 bits - long

Question #1:

Does anyone know how common sizes other than these are ? I know that the
standard says that number of bits in a "byte" (char) is >= 8, but I have not
come across values other than these yet ...

Question #2:

Is anyone aware of a package that has some portable definitions of things
like Int8, Int16 and Int32 (assuming they are possible for the platform) ?
If not, I think you could do this using the definitions in <limits.h>
(<climits> ?). I am aware that the preprocessor doesn''t understand the
sizeof operator, so you can''t do it that way.




我使用的是C编译器,默认情况下,int是8位。


就个人而言,我讨厌所有这些int,long,long long,double,

的业务双倍,所有这些难以理解的废话。 *和非便携式

废话,我可能会添加*。大多数编译器带有标题,我相信,

他们更加智能地定义数字类型,比如


__INT08

__INT16

....

__UINT08

__UINT16


等等。我自己没有使用它们,可能是因为额外的

打字(键盘输入我的意思),并且与现有代码一起玩。

必须有,虽然我不能肯定地告诉你,标准符合

和可理解的数字类型。


我当然希望有关于此的事情,并且很快就会实现,帮助我们

摆脱int所有这些。


你的。

dan



I work with a C compiler for which, by default, int is 8 bits.

Personally, I HATE all this business of int, long, long long, double,
double double, and all this incomprehensible crap. *And Non-Portable
Crap, I might add*. Most compilers come with headers, I believe,
where they define numeric types more inteligently, like

__INT08
__INT16
....
__UINT08
__UINT16

and so on. I haven''t used them myself, possibly because of the extra
typing (keyboard typing I mean), and to play along with existing code.
There must be, though I can''t tell you for sure, standard conforming
and comprehensible numeric types.

I sure hope something is done about this, and real soon, to help us
rid ourselves of "int" and all that.

Yours.
dan


" Jack Klein" ; < JA ******* @ spamcop.net>写道:
"Jack Klein" <ja*******@spamcop.net> wrote:
问题#2:

是否有人知道包含一些便携式定义
的包像Int8,Int16和Int32这样的东西(假设它们可以用于平台)
?如果没有,我认为你可以使用< limits.h>
(< climits>?)中的定义来做到这一点。我知道预处理器并不理解
sizeof运算符,所以你不能这样做。
当前的1999版C标准包括一个名为
的标题< stdint.h>包含各种整数类型的typedef。
所需的类型包括:int_least#_t,uint_least#_t,int_fastest#_t
和uint_fastest#_t,其中#至少包含8,16,32和64.
Question #2:

Is anyone aware of a package that has some portable definitions of things like Int8, Int16 and Int32 (assuming they are possible for the platform) ? If not, I think you could do this using the definitions in <limits.h>
(<climits> ?). I am aware that the preprocessor doesn''t understand the
sizeof operator, so you can''t do it that way.
The current 1999 version of the C standard includes a header named
<stdint.h> that contains typedefs for a variety of integer types. The
required types include: int_least#_t, uint_least#_t, int_fastest#_t
and uint_fastest#_t, where # consists of at least 8, 16, 32, and 64.




谢谢!


我没听说过< stdint.h>。任何其他新的&有用的标题附带C99?

我建议使用这些类型,因为它们可以在C
编译器中移植,并且几乎肯定包含在下一版本的C ++
标准中。



Thank you !

I hadn''t heard of <stdint.h>. Any other new & useful headers come with C99 ?
I would suggest using these types, as they will be portable across C
compilers and almost certainly included in the next version of the C++
standard.




听起来不错......有没有人知道C99和

C ++之间的合并和/或何时会发生这种情况?


只是为了感兴趣,这里是尝试一个可移植的头文件来定义

(U)Int8,(U)Int16和(U)Int32(缩进得到了被毁了)...


David F


---代码---


#ifndef INTEGER_H_INCLUDED

#define INTEGER_H_INCLUDED


//定义以下类型:

//

// Int8,UInt8,Int16,UInt16,Int32,UInt32

//

//如果基本类型不可用,此标题仍应编译,

//但如果实际使用了类型则会出错


#include< limits.h >


#if定义(INT8_S_TYPE)||已定义(INT8_U_TYPE)||已定义(INT16_TYPE)||

(INT32_TYPE)

#error"临时宏名称已在使用中

#endif


//找到8位整数类型


#if(UCHAR_MAX == 0xFF)

#define INT8_S_TYPE signed char

#define INT8_U_TYPE unsigned char

#endif


#if!defined(INT8_S_TYPE)&& (USHRT_MAX == 0xFF)

#define INT8_S_TYPE short

#define INT8_U_TYPE unsigned short

#endif


//找到16位整数类型


#if(USHRT_MAX == 0xFFFF)

#define INT16_TYPE short

#endif


#if!defined(INT16_TYPE)&& (UINT_MAX == 0xFFFF)

#define INT16_TYPE int

#endif


//找到32位整数类型


//如果没有32位类型,请尽量避免与非法值进行比较

#if(ULONG_MAX> 0xFFFF)

#if(ULONG_MAX> 0xFFFFFF)

#if(USHRT_MAX == 0xFFFFFFFF)

#define INT32_TYPE短

#endif

#if!defined(INT32_TYPE)&& (UINT_MAX == 0xFFFFFFFF)

#define INT32_TYPE int

#endif

#if!defined(INT32_TYPE)&& (UINT_MAX == 0xFFFFFFFF)

#define INT32_TYPE long

#endif

#endif

#endif // ULONG_MAX> 0xFFFF


//创建Int8 typedef

#ifdef INT8_S_TYPE

typedef INT8_S_TYPE Int8;

typedef INT8_U_TYPE UInt8;

#else

struct Int8Undefined;

typedef Int8Undefined Int8;

typedef Int8Undefined UInt8;

#endif // INT8_TYPE


//创建Int16 typedef


#ifdef INT16_TYPE

typedef INT16_TYPE Int16;

typedef unsigned INT16_TYPE UInt16;

#else

struct Int16Undefined;

typedef Int16Undefined Int16;

typedef Int16Undefined UInt16;

#endif // INT8_TYPE


//创建Int32 typedef


#ifdef INT32_TYPE

typedef INT32_TYPE Int32;

typedef unsigned INT32_TYPE UInt32;

#else

struct Int32Undefined;

typedef Int32Undefined Int32;

typedef Int32Undefined UInt32;

#endif // INT8_TYPE


// undef临时宏


#undef INT8_S_TYPE

#undef INT8_U_TYPE

#undef INT16_TYPE

#undef INT32_TYPE


#endif // INTEGER_H_INCLUDED


---结束代码---



Sounds good ... does anyone know how about proposed merges between C99 and
C++ and / or when this might happen ?

Just for interest, here is an attempt at a portable header file for defining
(U)Int8, (U)Int16 and (U)Int32 (indentation got destroyed) ...

David F

--- code ---

#ifndef INTEGER_H_INCLUDED
#define INTEGER_H_INCLUDED

// Defines the following types:
//
// Int8, UInt8, Int16, UInt16, Int32, UInt32
//
// If a fundamental type is not available, this header should still compile,
// but there will be an error if the type is actually used

#include <limits.h>

#if defined(INT8_S_TYPE) || defined(INT8_U_TYPE) || defined(INT16_TYPE) ||
defined(INT32_TYPE)
#error "Temporary macro name already in use"
#endif

// find 8 bit integer type

#if (UCHAR_MAX == 0xFF)
#define INT8_S_TYPE signed char
#define INT8_U_TYPE unsigned char
#endif

#if !defined(INT8_S_TYPE) && (USHRT_MAX == 0xFF)
#define INT8_S_TYPE short
#define INT8_U_TYPE unsigned short
#endif

// find 16 bit integer type

#if (USHRT_MAX == 0xFFFF)
#define INT16_TYPE short
#endif

#if !defined(INT16_TYPE) && (UINT_MAX == 0xFFFF)
#define INT16_TYPE int
#endif

// find 32 bit integer type

// try to avoid comparing to an illegal value if there is no 32 bit type
#if (ULONG_MAX > 0xFFFF)
#if (ULONG_MAX > 0xFFFFFF)
#if (USHRT_MAX == 0xFFFFFFFF)
#define INT32_TYPE short
#endif
#if !defined(INT32_TYPE) && (UINT_MAX == 0xFFFFFFFF)
#define INT32_TYPE int
#endif
#if !defined(INT32_TYPE) && (UINT_MAX == 0xFFFFFFFF)
#define INT32_TYPE long
#endif
#endif
#endif // ULONG_MAX > 0xFFFF

// create Int8 typedef

#ifdef INT8_S_TYPE
typedef INT8_S_TYPE Int8;
typedef INT8_U_TYPE UInt8;
#else
struct Int8Undefined;
typedef Int8Undefined Int8;
typedef Int8Undefined UInt8;
#endif // INT8_TYPE

// create Int16 typedef

#ifdef INT16_TYPE
typedef INT16_TYPE Int16;
typedef unsigned INT16_TYPE UInt16;
#else
struct Int16Undefined;
typedef Int16Undefined Int16;
typedef Int16Undefined UInt16;
#endif // INT8_TYPE

// create Int32 typedef

#ifdef INT32_TYPE
typedef INT32_TYPE Int32;
typedef unsigned INT32_TYPE UInt32;
#else
struct Int32Undefined;
typedef Int32Undefined Int32;
typedef Int32Undefined UInt32;
#endif // INT8_TYPE

// undefine temporary macros

#undef INT8_S_TYPE
#undef INT8_U_TYPE
#undef INT16_TYPE
#undef INT32_TYPE

#endif // INTEGER_H_INCLUDED

--- end code ---


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

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