It seems all of them take 4 bytes of space,
看起来它们都占用了4字节的空间,
so what's the difference?
所以有什么区别呢?
3 个解决方案
#1
23
First of all, the size of int/long is unspecified. So on your compiler, an int
and a long
might be the same, but this isn't universal across compilers.
首先,不指定int/long的大小。所以在编译器上,int和long可能是相同的,但是在编译器之间并不是通用的。
As for the difference between unsigned long
and long
:
关于无符号long和long的区别:
Assuming 4 bytes, a long
has the range of -2,147,483,648
to 2,147,483,647
. An unsigned long has the range of 0
to 4,294,967,295
.
假设4个字节,一个long的范围是-2,147,483,648到2,147,483,647。无符号long的范围为0到4,294,967,295。
One other difference is with overflow. For a signed type, an overflow has unspecified behavior. But for an unsigned type, overflow is guaranteed to "wrap around."
另一个区别是溢出。对于签名类型,溢出具有未指定的行为。但是对于无符号类型,溢出被保证为“环绕”。
#2
9
The C language specification allows the implementation of int and long types to vary from one platform to another within a few constraints. This variability is a headache for cross-platform code, but it is also an asset because it enables the informed programmer to balance their design goals between native processor speed and full numeric range on hardware architectures that don't offer both.
C语言规范允许在一些约束条件下实现int和long类型,在不同的平台之间变化。这种可变性对于跨平台代码来说是一个令人头痛的问题,但它也是一项资产,因为它使见多识广的程序员能够在本地处理器速度和硬件体系结构上的全数字范围之间进行平衡,而硬件体系结构并不能同时提供这两种功能。
In general, "int" is supposed to map a machine register size of the target CPU architecture's machine, so that loading, storing, and operating on the int type data should translate directly into operations that use the target processor's native registers.
一般来说,“int”应该映射目标CPU体系结构的计算机的机器寄存器大小,以便在int类型数据上加载、存储和操作应该直接转换为使用目标处理器的本机寄存器的操作。
Int can be less than the machine register size in the interest of saving memory space (big ints take up twice as much RAM as little ints). It's common to see int as a 32 bit entity even on 64 bit architectures where compatibility with older systems and memory efficiency are high priorities.
为了节省内存空间,Int可以小于机器寄存器的大小(大ints占用的内存是小ints的两倍)。即使在64位体系结构中,int作为一个32位的实体也很常见,在这些体系结构中,与旧系统的兼容性和内存效率是最重要的。
"long" can be the same size or larger than "int" depending on the target architecture's register sizes. Operations on "long" may be implemented in software if the target architecture doesn't support values that large in its native machine registers.
“long”可以是相同的大小,也可以大于“int”,这取决于目标体系结构的寄存器大小。如果目标体系结构不支持本地机器寄存器中那么大的值,那么“长”操作可以在软件中实现。
CPU chips designed for power efficiency or embedded devices are where you will find distinctions between int and long these days. Compilers for general purpose CPUs like in your desktop or laptop PC generally treat int and long as the same size because the CPU efficiently uses 32 bit registers. On smaller devices such as cell phones the CPU may be built to handle 16 bit data more naturally and have to work hard to handle 32 bit or larger data.
CPU芯片设计的电源效率或嵌入式设备,您将在这里发现int和long之间的区别。用于一般用途CPU的编译器,如桌面或笔记本电脑中的编译器,通常处理int和长时间,因为CPU有效地使用32位寄存器。在像手机这样的小型设备上,CPU可以更自然地处理16位数据,并且必须努力处理32位或更大的数据。
Fewer bits per register means fewer circuits required on the chip, fewer data lines to move data in and out of the chip, lower power consumption and smaller chip die size, all of which make for a lower cost (in $ and in watts) device.
每个寄存器的比特数越少,意味着芯片上所需的电路就越少,进出芯片的数据线就越少,功耗就越低,芯片的芯片模量也就越小,所有这些都降低了设备的成本(以美元和瓦数计算)。
In such an architecture, you will most likely find int to be 16 bits in size and long to be 32 bits in size. There may also be a performance penalty associated with using longs, caused by either wait states to load the 32 bits in multiple reads across a 16 bit data bus, or caused by implementing long operations (addition, subtraction, etc) in software if the native hardware doesn't support such operations in hardware.
在这样的架构中,您很可能会发现int的大小是16位,长是32位。也可能有一个性能与使用多头,要么等待状态引起的加载在多个读取32位16位数据总线,或由于实现长操作(加法、减法等)在软件如果本机硬件不支持这样的操作在硬件。
As a general rule, the only thing you can assume about ints and longs is that the range of int should always be less than or equal to long on any architecture. You should also assume that someday your code will be recompiled for a different architecture where whatever relationship you currently see between int and long no longer exists.
一般来说,对于int类型和long类型,您只能假定int类型的范围在任何体系结构上都应该小于或等于long。您还应该假设有一天您的代码将被重新编译为一个不同的体系结构,在这个体系结构中,您当前看到的int和long之间的关系将不复存在。
This is why you should be careful to keep ints separate from longs even in everyday mundane coding. They may be completely assignment compatible today because their implementation details for your current hardware platform coincide, but that coincidence is not guaranteed across all platforms.
这就是为什么您应该小心地将ints与long分隔开,即使在日常的编码中也是如此。它们今天可能完全兼容分配,因为它们在当前硬件平台的实现细节是一致的,但并非所有平台都能保证这种一致性。
#3
3
Well, the difference between unsigned long
and long
is simple -- the upper bound. Signed long
goes from (on an average 32-bit system) about -2.1 billion (-2^31) to +2.1 billion (+2^31 - 1), while unsigned long
goes from 0 to 4.2 billion (2^32 - 1).
无符号长和长之间的区别很简单——上界。签署长期从(平均32位系统)-21亿(2 ^ 31)+ 21亿(+ 2 ^ 31 - 1),而无符号长从0到42亿(2 ^ 32 - 1)。
It so happens that on many compilers and operating systems (including, apparently, yours), int
is also a 32-bit value. But the C++ standard doesn't determine maximum widths for any of these types, only minimum widths. On some systems, int
is 16 bits. On some systems, long
is 64 bits. A lot of it depends on the processor architecture being targeted, and what its base word size is.
在许多编译器和操作系统(显然包括您的)上,int也是一个32位的值。但是c++标准并没有确定这些类型的最大宽度,只有最小宽度。在某些系统中,int是16位。在某些系统中,长是64位。这在很大程度上取决于目标处理器体系结构,以及它的基本字大小。
The header limits.h
exists to define the maximum capacity of the various types under the current compilation environment, and stdint.h
exists to provide environment-independent types of guaranteed width, such as int32_t
.
头的限制。h的存在是为了定义当前编译环境下各种类型的最大容量,以及stdint。h的存在是为了提供与环境无关的保证宽度类型,如int32_t。
#1
23
First of all, the size of int/long is unspecified. So on your compiler, an int
and a long
might be the same, but this isn't universal across compilers.
首先,不指定int/long的大小。所以在编译器上,int和long可能是相同的,但是在编译器之间并不是通用的。
As for the difference between unsigned long
and long
:
关于无符号long和long的区别:
Assuming 4 bytes, a long
has the range of -2,147,483,648
to 2,147,483,647
. An unsigned long has the range of 0
to 4,294,967,295
.
假设4个字节,一个long的范围是-2,147,483,648到2,147,483,647。无符号long的范围为0到4,294,967,295。
One other difference is with overflow. For a signed type, an overflow has unspecified behavior. But for an unsigned type, overflow is guaranteed to "wrap around."
另一个区别是溢出。对于签名类型,溢出具有未指定的行为。但是对于无符号类型,溢出被保证为“环绕”。
#2
9
The C language specification allows the implementation of int and long types to vary from one platform to another within a few constraints. This variability is a headache for cross-platform code, but it is also an asset because it enables the informed programmer to balance their design goals between native processor speed and full numeric range on hardware architectures that don't offer both.
C语言规范允许在一些约束条件下实现int和long类型,在不同的平台之间变化。这种可变性对于跨平台代码来说是一个令人头痛的问题,但它也是一项资产,因为它使见多识广的程序员能够在本地处理器速度和硬件体系结构上的全数字范围之间进行平衡,而硬件体系结构并不能同时提供这两种功能。
In general, "int" is supposed to map a machine register size of the target CPU architecture's machine, so that loading, storing, and operating on the int type data should translate directly into operations that use the target processor's native registers.
一般来说,“int”应该映射目标CPU体系结构的计算机的机器寄存器大小,以便在int类型数据上加载、存储和操作应该直接转换为使用目标处理器的本机寄存器的操作。
Int can be less than the machine register size in the interest of saving memory space (big ints take up twice as much RAM as little ints). It's common to see int as a 32 bit entity even on 64 bit architectures where compatibility with older systems and memory efficiency are high priorities.
为了节省内存空间,Int可以小于机器寄存器的大小(大ints占用的内存是小ints的两倍)。即使在64位体系结构中,int作为一个32位的实体也很常见,在这些体系结构中,与旧系统的兼容性和内存效率是最重要的。
"long" can be the same size or larger than "int" depending on the target architecture's register sizes. Operations on "long" may be implemented in software if the target architecture doesn't support values that large in its native machine registers.
“long”可以是相同的大小,也可以大于“int”,这取决于目标体系结构的寄存器大小。如果目标体系结构不支持本地机器寄存器中那么大的值,那么“长”操作可以在软件中实现。
CPU chips designed for power efficiency or embedded devices are where you will find distinctions between int and long these days. Compilers for general purpose CPUs like in your desktop or laptop PC generally treat int and long as the same size because the CPU efficiently uses 32 bit registers. On smaller devices such as cell phones the CPU may be built to handle 16 bit data more naturally and have to work hard to handle 32 bit or larger data.
CPU芯片设计的电源效率或嵌入式设备,您将在这里发现int和long之间的区别。用于一般用途CPU的编译器,如桌面或笔记本电脑中的编译器,通常处理int和长时间,因为CPU有效地使用32位寄存器。在像手机这样的小型设备上,CPU可以更自然地处理16位数据,并且必须努力处理32位或更大的数据。
Fewer bits per register means fewer circuits required on the chip, fewer data lines to move data in and out of the chip, lower power consumption and smaller chip die size, all of which make for a lower cost (in $ and in watts) device.
每个寄存器的比特数越少,意味着芯片上所需的电路就越少,进出芯片的数据线就越少,功耗就越低,芯片的芯片模量也就越小,所有这些都降低了设备的成本(以美元和瓦数计算)。
In such an architecture, you will most likely find int to be 16 bits in size and long to be 32 bits in size. There may also be a performance penalty associated with using longs, caused by either wait states to load the 32 bits in multiple reads across a 16 bit data bus, or caused by implementing long operations (addition, subtraction, etc) in software if the native hardware doesn't support such operations in hardware.
在这样的架构中,您很可能会发现int的大小是16位,长是32位。也可能有一个性能与使用多头,要么等待状态引起的加载在多个读取32位16位数据总线,或由于实现长操作(加法、减法等)在软件如果本机硬件不支持这样的操作在硬件。
As a general rule, the only thing you can assume about ints and longs is that the range of int should always be less than or equal to long on any architecture. You should also assume that someday your code will be recompiled for a different architecture where whatever relationship you currently see between int and long no longer exists.
一般来说,对于int类型和long类型,您只能假定int类型的范围在任何体系结构上都应该小于或等于long。您还应该假设有一天您的代码将被重新编译为一个不同的体系结构,在这个体系结构中,您当前看到的int和long之间的关系将不复存在。
This is why you should be careful to keep ints separate from longs even in everyday mundane coding. They may be completely assignment compatible today because their implementation details for your current hardware platform coincide, but that coincidence is not guaranteed across all platforms.
这就是为什么您应该小心地将ints与long分隔开,即使在日常的编码中也是如此。它们今天可能完全兼容分配,因为它们在当前硬件平台的实现细节是一致的,但并非所有平台都能保证这种一致性。
#3
3
Well, the difference between unsigned long
and long
is simple -- the upper bound. Signed long
goes from (on an average 32-bit system) about -2.1 billion (-2^31) to +2.1 billion (+2^31 - 1), while unsigned long
goes from 0 to 4.2 billion (2^32 - 1).
无符号长和长之间的区别很简单——上界。签署长期从(平均32位系统)-21亿(2 ^ 31)+ 21亿(+ 2 ^ 31 - 1),而无符号长从0到42亿(2 ^ 32 - 1)。
It so happens that on many compilers and operating systems (including, apparently, yours), int
is also a 32-bit value. But the C++ standard doesn't determine maximum widths for any of these types, only minimum widths. On some systems, int
is 16 bits. On some systems, long
is 64 bits. A lot of it depends on the processor architecture being targeted, and what its base word size is.
在许多编译器和操作系统(显然包括您的)上,int也是一个32位的值。但是c++标准并没有确定这些类型的最大宽度,只有最小宽度。在某些系统中,int是16位。在某些系统中,长是64位。这在很大程度上取决于目标处理器体系结构,以及它的基本字大小。
The header limits.h
exists to define the maximum capacity of the various types under the current compilation environment, and stdint.h
exists to provide environment-independent types of guaranteed width, such as int32_t
.
头的限制。h的存在是为了定义当前编译环境下各种类型的最大容量,以及stdint。h的存在是为了提供与环境无关的保证宽度类型,如int32_t。