NSInteger 类型
我想知道整数 16、整数 32 和整数 64 之间的区别,以及有符号整数和无符号整数(NSInteger 和 NSUInteger)之间的区别
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定“整数 16”、“整数 32”和“整数 64”到底是什么类型,但通常这些数字指的是整数类型的位大小。
有符号整数和无符号整数之间的区别在于它可以表示的值的范围。例如,二进制补码有符号 16 位整数可以表示 -32,768 到 32,767 之间的数字。无符号 16 位整数可以表示 0 到 65,535 之间的值。
对于当今使用的大多数计算机来说,宽度为 n 的有符号整数可以表示值 [-2n-1,2< em>n-1) 和宽度 n 的无符号整数可以表示值 [0,2n )。
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.
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.
For most computers in use today, a signed integer of width n can represent the values [-2n-1,2n-1) and an unsigned integer of width n can represent values [0,2n).
NSInteger 和 NSUInteger 是 Apple 的自定义整数数据类型。前者已签名,后者未签名。在 32 位版本上,NSInteger 的类型定义为 int,而在 64 位版本上,它的类型定义为 long。 NSUInteger 的类型定义为 32 位的 unsigned int 和 64 位的 unsigned long。有符号类型涵盖范围 [-2^(n-1), 2^(n-1)],其中 n 是位值,无符号类型涵盖范围 [0, 2^n]。
在为单个独立程序进行编码时,使用 NSInteger 或 NSUInteger 被认为是面向未来的平台位更改的最佳实践。在处理固定大小的数据需求(例如二进制文件格式或网络)时,这不是最佳实践,因为所需的字段宽度是预先定义的并且无论平台位级别如何都是恒定的。这就是 stdint.h 中定义的固定大小类型(即 uint8_t、uint16_t、uint32_t 等)发挥作用的地方。
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].
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.
无符号与有符号整数 -
无符号通常用于不允许变量采用负数的情况。例如,在循环遍历数组时,如果数组下标变量是 unsigned int 并且循环直到数组的长度,那么它总是有用/可读的。
另一方面,如果变量也可以有负数,则将变量声明为有符号 int。默认情况下,整数变量是有符号的。
Unsigned vs signed integer -
Unsigned is 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.
On the other hand, if variable can have negative numbers too then declare the variable as signed int. Integer variables are signed by default.
查看基金会数据类型。 NInteger 和 NSUInteger 以及 int 和 unsigned int 的 typedef。
来自维基百科
,这意味着您通常必须使用一个位来对符号进行编码,从而减少您可以表示的数字范围。
Have a look at the Foundation Data types. NInteger and NSUInteger and typedef for int and unsigned int.
From wikipedia
which means that you normally have to use a bit to encode the sign thus reducing the range in of number you can represent.