xcode NSInteger 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4925511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:11:42  来源:igfitidea点击:

NSInteger types

iphoneobjective-cxcodetypesinteger

提问by Skullhouseapps

I would like to know what is the differecnce between Integer 16, Integer 32 and Integer 64, and the difference between a signed integer and an unsigned integer(NSInteger and NSUInteger)

我想知道整数 16、整数 32 和整数 64 之间的区别,以及有符号整数和无符号整数(NSInteger 和 NSUInteger)之间的区别

回答by Carl Norum

I'm not sure exactly what types you mean by "Integer 16", "Integer 32", and "Integer 64", but normally, those numbers refer to the size in bits of the integer type.

我不确定“整数 16”、“整数 32”和“整数 64”究竟是什么类型,但通常,这些数字指的是整数类型的位大小。

The difference between a signed and an unsigned integer is the range of values it can represent. For example, a two's-complement signed 16-bit integer can represent numbers between -32,768 and 32,767. An unsigned 16-bit integer can represent values between 0 and 65,535.

有符号和无符号整数之间的区别在于它可以表示的值的范围。例如,二进制补码有符号 16 位整数可以表示 -32,768 和 32,767 之间的数字。一个无符号的 16 位整数可以表示 0 到 65,535 之间的值。

For most computers in use today, a signed integer of width ncan represent the values [-2n-1,2n-1) and an unsigned integer of width ncan represent values [0,2n).

对于当今使用的大多数计算机,宽度为n 的有符号整数可以表示值 [-2 n-1,2 n-1),宽度为n的无符号整数可以表示值 [0,2 n)。

回答by Ryan

NSInteger and NSUInteger are Apple's custom integer data types. The first is signed while the latter is unsigned. On 32-bit builds NSInteger is typedef'd as an int while on 64-bit builds it's typedef'd as a long. NSUInteger is typedef'd as an unsigned int for 32-bit and an unsigned long for 64-bit. Signed types cover the range [-2^(n-1), 2^(n-1)] where n is the bit value and unsigned types cover the range [0, 2^n].

NSInteger 和 NSUInteger 是 Apple 的自定义整数数据类型。第一个是有符号的,而后者是无符号的。在 32 位构建中,NSInteger 被 typedef 为 int,而在 64 位构建中,它被 typedef 为 long。NSUInteger 被 typedef 定义为 32 位的 unsigned int 和 64 位的 unsigned long。有符号类型覆盖范围 [-2^(n-1), 2^(n-1)],其中 n 是位值,无符号类型覆盖范围 [0, 2^n]。

When coding for a single, self-contained program, using NSInteger or NSUInteger are considered the best practice for future-proofing against platform bit changes. It is not the best practice when dealing with fixed-size data needs, such as with binary file formats or networking, because the required field widths are defined previously and constant regardless of the platform bit level. This is where the fixed-size types defined in stdint.h (i.e., uint8_t, uint16_t, uint32_t, etc) come into use.

在为单个独立程序编码时,使用 NSInteger 或 NSUInteger 被认为是针对平台位更改的未来验证的最佳实践。在处理固定大小的数据需求(例如二进制文件格式或网络)时,这不是最佳实践,因为所需的字段宽度是先前定义的,并且无论平台位级别如何都是恒定的。这是 stdint.h 中定义的固定大小类型(即 uint8_t、uint16_t、uint32_t 等)开始使用的地方。

回答by Mahesh

Unsigned vs signed integer -

无符号与有符号整数 -

Unsignedis usually used where variables aren't allowed to take negative numbers. For example, while looping through an array, its always useful/readable if the array subscript variable is unsigned int and loop through until the length of the array.

无符号通常用于不允许变量取负数的地方。例如,在循环遍历数组时,如果数组下标变量是 unsigned int 并循环到数组的长度,则它总是有用/可读的。

On the other hand, if variable can have negative numbers too then declare the variable as signed int. Integer variables are signed by default.

另一方面,如果变量也可以有负数,则将变量声明为有符号整数。默认情况下,整数变量是有符号的。

回答by Rod

Have a look at the Foundation Data types. NInteger and NSUInteger and typedef for int and unsigned int.

看看基础数据类型。NInteger 和 NSUInteger 以及 int 和 unsigned int 的 typedef。

From wikipedia

来自维基百科

In computing, signed number representations are required to encode negative numbers in binary number systems

在计算中,需要有符号数表示来对二进制数系统中的负数进行编码

which means that you normally have to use a bit to encode the sign thus reducing the range in of number you can represent.

这意味着您通常必须使用一点来对符号进行编码,从而减少您可以表示的数字范围。