我读到 int 的范围取决于字节。
所以将 int 设为 4 个字节长,即 4 * 8 位 = 32 位。
所以范围应该是:2 ^ (32-1) = 2 ^ (31)
为什么有些人说它是 2^31 - 1?
谢谢!
最佳答案
因为从0开始计数
而int的范围是2,147,483,647和2^32即2,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/