c - 为什么int的范围有负1?

标签 c numbers int range number-systems

我读到 int 的范围取决于字节。

所以将 int 设为 4 个字节长,即 4 * 8 位 = 32 位。

所以范围应该是:2 ^ (32-1) = 2 ^ (31)

为什么有些人说它是 2^31 - 1?

谢谢!

最佳答案

因为从0开始计数

而int的范围是2,147,483,6472^322,147,483,648。因此我们减去 1

同样丢1bit是为了正负号

检查此兴趣 inf wiki关于整数的文章:-

The most common representation of a positive integer is a string of bits, using the binary numeral system. The order of the memory bytes storing the bits varies; see endianness. The width or precision of an integral type is the number of bits in its representation. An integral type with n bits can encode 2n numbers; for example an unsigned type typically represents the non-negative values 0 through 2n−1. Other encodings of integer values to bit patterns are sometimes used, for example Binary-coded decimal or Gray code, or as printed character codes such as ASCII.

There are four well-known ways to represent signed numbers in a binary computing system. The most common is two's complement, which allows a signed integral type with n bits to represent numbers from −2(n−1) through 2(n−1)−1. Two's complement arithmetic is convenient because there is a perfect one-to-one correspondence between representations and values (in particular, no separate +0 and −0), and because addition, subtraction and multiplication do not need to distinguish between signed and unsigned types. Other possibilities include offset binary, sign-magnitude, and ones' complement.

关于c - 为什么int的范围有负1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19333581/

相关文章:

python - 您将如何实现除数函数?

c - 结构内的 union 。编译警告。 C

c - 将结构指针传递给函数时出现错误 "ISO C forbids forward parameter decleration"?

JavaFX DoubleProperty intValue() 方法始终返回 0,其中返回 0....100

mysql - 美国邮政编码的最佳列类型是什么?

c# - 制作一个随机整数数组

string - 如何将整数转换为字符串?

C - 带有函数内 malloc 的 typedef 结构的动态数组

c - Valgrind 显示 OS X 上的 hello world 程序有超过 200 个分配?

R-如何按行索引号对数据帧进行重新排序