为什么int的范围是-32768到32767? [英] Why the range of int is -32768 to 32767?

查看:222
本文介绍了为什么int的范围是-32768到32767?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么任何数据类型的范围更大的消极的一面是比较积极的一面?

例如,在整数的情况:

的Turbo C 的范围为 -32768 32767 和<强> Visual Studio中是 -2147483648 2147483647

同样的情况为其他数据类型...

The same happens to other data types...

[UPD:用于设置适当的限制值的Visual Studio ]

[UPD: Set proper limit values for Visual Studio]

推荐答案

。数字签名是使用所谓的二进制补码。

Because of how numbers are stored. Signed numbers are stored using something called "two's complement notation".

记住所有的变量有一定量的位。如果它们中的最显著之一,在左侧的之一,是0,那么这个数是非负(即,正或零),并且各个位的其余部分简单地重新present的值。

Remember all variables have a certain amount of bits. If the most significant one of them, the one on the left, is a 0, then the number is non-negative (i.e., positive or zero), and the rest of the bits simply represent the value.

然而,如果最左边的位是1,则该数目是负的。数的实际值可以通过从整个数重新psented $ P $减去2 ^ N得到(为一个无符号的数量,其中最左边的1),其中n是位的数量的变量。

However, if the leftmost bit is a 1, then the number is negative. The real value of the number can be obtained by subtracting 2^n from the whole number represented (as an unsigned quantity, including the leftmost 1), where n is the amount of bits the variable has.

由于只有N - 1比特留给实际值(以下简称尾数)的数量,可能的组合是2 ^(N - 1)。对于正/零的数字,这是很容易的:他们从0到2 ^(N - 1) - 1,这是-1考虑到零本身 - 例如,如果你只有四种可能的组合,这些组合将重新present 0,1,2,3(注意如何有四个数字):它从0到4 - 1

Since only n - 1 bits are left for the actual value (the "mantissa") of the number, the possible combinations are 2^(n - 1). For positive/zero numbers, this is easy: they go from 0, to 2^(n - 1) - 1. That -1 is to account for zero itself -- for instance, if you only had four possible combinations, those combinations would represent 0, 1, 2, and 3 (notice how there's four numbers): it goes from 0 to 4 - 1.

对于负数,记得最左边位为1,因此整个数字再presented去2 ^(N - 1)与(2 ^ N) - 1(括号中是非常重要的存在!)。不过,正如我说的,你必须要2 ^ N客场拿到多少实际价值。 2 ^(N - 1) - 2 ^ N为 - (2 ^(N - 1))和((2 ^ N) - 1) - 2 ^ n为-1。因此,负数'范围是 - (2 ^(N - 1))。为-1

For negative numbers, remember the leftmost bit is 1, so the whole number represented goes between 2^(n - 1) and (2^n) - 1 (parentheses are very important there!). However, as I said, you have to take 2^n away to get the real value of the number. 2^(n - 1) - 2^n is -(2^(n - 1)), and ((2^n) - 1) - 2^n is -1. Therefore, the negative numbers' range is -(2^(n - 1)) to -1.

把所有的一起,你会得到-2 ^(N - 1)2 ^(N - 1) - 1,正如你所看到的,上限得到下界做了-1不是

Put all that together and you get -2^(n - 1) to 2^(n - 1) - 1. As you can see, the upper bound gets a -1 that the lower bound doesn't.

这就是为什么有比正面多了一个负数。

And that's why there's one more negative number than positive.

这篇关于为什么int的范围是-32768到32767?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆