注册 登录
编程论坛 C++教室

求教:为什么int 型数据上限是比下限少一个呢?

andrewpoon 发布于 2007-09-24 14:59, 4256 次点击
1.对于int型数据,若是负数,第0位为1;正数则为0.那么32位机上范围是-2,147,483,648~~2,147,483,647.
问题一:为什么负数比正数多一个呢?
问题二:最小负数-2,147,483,648转换成而二进制是怎么样的呢?
问题三:那int 型数据二进制数字 1000 0000 0000 0000 0000 0000 0000 0000转换成十进制是多少呢?
14 回复
#2
雨中飞燕2007-09-24 15:07
你应当把0当成“正数”(最高符号位为0表示非负)



by 雨中飞燕 QQ:78803110 C/C++讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
#3
PcrazyC2007-09-25 00:46
问题并不是楼上所说的,你去看看原码,补码的资料吧,计算机里储存的都是以补码形式存在的,假设INT是32位的,10^32(最高位是1,其它位是0)正好是负数-10^31的补码形式,而正数最大只能是10^31-1(最高位是0,其它位是1,正数补码与原码相同)
#4
雨中飞燕2007-09-25 01:01
以下是引用PcrazyC在2007-9-25 0:46:05的发言:
问题并不是楼上所说的,你去看看原码,补码的资料吧,计算机里储存的都是以补码形式存在的,假设INT是32位的,10^32(最高位是1,其它位是0)正好是负数-10^31的补码形式,而正数最大只能是10^31-1(最高位是0,其它位是1,正数补码与原码相同)

为什么不是??按照符号位的定义,0不就是“正数”吗?
这样不就是正负一样多吗?0 ~ 2^n-1 和 -1 ~ 2^n 不就是一样多吗?



by 雨中飞燕 QQ:78803110 C/C++讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/

#5
缘吇弹2007-09-25 01:18
0不是正数也不是负数

#6
缘吇弹2007-09-25 01:19
正数>0>负数
#7
雨中飞燕2007-09-25 02:32
以下是引用缘吇弹在2007-9-25 1:19:01的发言:
正数>0>负数

请你搞清楚我4楼是说的什么,不要无故反驳,连鬼都知道0不是正数



by 雨中飞燕 QQ:78803110 C/C++讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/

[此贴子已经被作者于2007-9-25 2:39:33编辑过]

#8
valentineyzq2007-09-25 08:51
我记得0的符号位是0,按符号位定义的话0也是正数。

[此贴子已经被作者于2007-9-25 8:51:54编辑过]


#9
缘吇弹2007-09-25 13:07
回复:(雨中飞燕)以下是引用缘吇弹在2007-9-25 1:19...

那不是在反驳你啦

#10
PcrazyC2007-09-25 13:12
为什么int 型数据上限是比下限少一个呢?

单纯的回答这个问题,二楼的还是可以的

我在三楼只是从实质上分析了一下
#11
nuciewth2007-09-25 13:51
以下是引用andrewpoon在2007-9-24 14:59:02的发言:
1.对于int型数据,若是负数,第0位为1;正数则为0.那么32位机上范围是-2,147,483,648~~2,147,483,647.
问题一:为什么负数比正数多一个呢?
问题二:最小负数-2,147,483,648转换成而二进制是怎么样的呢?
问题三:那int 型数据二进制数字 1000 0000 0000 0000 0000 0000 0000 0000转换成十进制是多少呢?

1.计算机内部存储是按补码的形式存储的,最高位为0表示正数,其原码就是补码本身.
最高位为1表示负数.而0占一个补码的(000 ... 000).这就是为什么正数少一个,因为0的最高位也是0.
2.根据补码最小负数应该是(以8位举例)10000000(按位取反为1111 1111,再加1为1000 0000,真值为-256)
3.还是按位取反再加1 1000 0000 --->1111 1111--->1000 0000(真值-256此时,最高位是符号位也是数值位).

注意一下在真值1000 0000表示的时候最高位1是数值位.而补码不同(应该按照转换规则,是什么就是什么).

#12
yuyunliuhen2007-09-25 14:25

Upstair is right!

complementarity:
// Numeric.cpp
#include <iostream>
#include <limits>

using namespace std;

int main() {
cout << " 1 The minimum value for char is " <<
(int)numeric_limits<char>::min() << endl;
cout << " 2 The minimum value for int is " <<
numeric_limits<int>::min() << endl;
cout << " 3 The maximum value for char is " <<
(int)numeric_limits<char>::max() << endl;
cout << " 4 The maximum value for int is " <<
numeric_limits<int>::max() << endl;
cout << " 5 The number of bits to represent a char is " <<
numeric_limits<char>::digits << endl;
cout << " 6 The number of bits to represent an int is " <<
numeric_limits<int>::digits << endl;
cout <<" 7 The number of digits representable in base 10 for float is "
<< numeric_limits<float>::digits10 << endl;
cout << " 8 Is a char signed? " <<
numeric_limits<char>::is_signed << endl;
cout << " 9 Is an unsigned integer signed? " <<
numeric_limits<unsigned int>::is_signed << endl;
cout << "10 Is an integer an integer? " <<
numeric_limits<int>::is_integer << endl;
cout << "11 Is a float an integer? " <<
numeric_limits<float>::is_integer << endl;
cout << "12 Is an integer exact? " <<
numeric_limits<int>::is_exact << endl;
cout << "13 Is a float exact? " <<
numeric_limits<float>::is_exact << endl;
cout << "14 The radix for float is " <<
numeric_limits<float>::radix << endl;
cout << "15 The epsilon for float is " <<
numeric_limits<float>::epsilon() << endl;
cout << "16 The round error for float is " <<
numeric_limits<float>::round_error() << endl;
cout << "17 The minimum exponent for float is " <<
numeric_limits<float>::min_exponent << endl;
cout << "18 The minimum exponent in base 10 " <<
numeric_limits<float>::min_exponent10 << endl;
cout << "19 The maximum exponent is " <<
numeric_limits<float>::max_exponent << endl;
cout << "20 The maximum exponent in base 10 " <<
numeric_limits<float>::max_exponent10 << endl;
cout << "21 Can float represent positive infinity? " <<
numeric_limits<float>::has_infinity << endl;
cout << "22 Can double represent positive infinity? " <<
numeric_limits<double>::has_infinity << endl;
cout << "23 Can int represent positive infinity? " <<
numeric_limits<int>::has_infinity << endl;
cout << "24 Can float represent a NaN? " <<
numeric_limits<float>::has_quiet_NaN << endl;
cout << "25 Can float represent a signaling NaN? " <<
numeric_limits<float>::has_signaling_NaN << endl;
cout << "26 Does float allow denormalized values? " <<
numeric_limits<float>::has_denorm << endl;
cout << "27 Does float detect denormalization loss? " <<
numeric_limits<float>::has_denorm_loss << endl;
cout << "28 Representation of positive infinity for float " <<
numeric_limits<float>::infinity() << endl;
cout << "29 Representation of quiet NaN for float " <<
numeric_limits<float>::quiet_NaN() << endl;
cout << "30 Minimum denormalized number for float " <<
numeric_limits<float>::denorm_min() << endl;
cout << "31 Minimum positive denormalized value for float " <<
numeric_limits<float>::denorm_min() << endl;
cout << "32 Does float adhere to IEC 559 standard? " <<
numeric_limits<float>::is_iec559 << endl;
cout << "33 Is float bounded? " <<
numeric_limits<float>::is_bounded << endl;
cout << "34 Is float modulo? " <<
numeric_limits<float>::is_modulo << endl;
cout << "35 Is int modulo? " <<
numeric_limits<float>::is_modulo << endl;
cout << "36 Is trapping implemented for float? " <<
numeric_limits<float>::traps << endl;
cout << "37 Is tinyness detected before rounding? " <<
numeric_limits<float>::tinyness_before << endl;
cout << "38 What is the rounding style for float? " <<
(int)numeric_limits<float>::round_style << endl;
cout << "39 What is the rounding style for int? " <<
(int)numeric_limits<int>::round_style << endl;
cout << "40 How does a float represent a signaling NaN? " <<
numeric_limits<float>::signaling_NaN() << endl;
cout << "41 Is int specialized? " <<
numeric_limits<float>::is_specialized << endl;
}
////////////////////////////////////////////////////////////////////////////
Output

1 The minimum value for char is -128
2 The minimum value for int is -2147483648
3 The maximum value for char is 127
4 The maximum value for int is 2147483647
5 The number of bits to represent a char is 7
6 The number of bits to represent an int is 31
7 The number of digits representable in base 10 for float is 6
8 Is a char signed? 1
9 Is an unsigned integer signed? 0
10 Is an integer an integer? 1
11 Is a float an integer? 0
12 Is an integer exact? 1
13 Is a float exact? 0
14 The radix for float is 2
15 The epsilon for float is 1.19209e-007
16 The round error for float is 0.5
17 The minimum exponent for float is -125
18 The minimum exponent in base 10 -37
19 The maximum exponent is 128
20 The maximum exponent in base 10 38
21 Can float represent positive infinity? 1
22 Can double represent positive infinity? 1
23 Can int represent positive infinity? 0
24 Can float represent a NaN? 1
25 Can float represent a signaling NaN? 1
26 Does float allow denormalized values? 1
27 Does float detect denormalization loss? 1
28 Representation of positive infinity for float 1.#INF
29 Representation of quiet NaN for float 1.#QNAN
30 Minimum denormalized number for float 1.4013e-045
31 Minimum positive denormalized value for float 1.4013e-045
32 Does float adhere to IEC 559 standard? 1
33 Is float bounded? 1
34 Is float modulo? 0
35 Is int modulo? 0
36 Is trapping implemented for float? 1
37 Is tinyness detected before rounding? 1
38 What is the rounding style for float? 1
39 What is the rounding style for int? 0
40 How does a float represent a signaling NaN? 1.#QNAN
41 Is int specialized? 1

#13
承诺总是很短2007-09-25 23:43
回复:(andrewpoon)求教:为什么int 型数据上限是比...
很多计算机入门教程都有解释的,计组,C.......
弄清楚反码,补码就OK了
#14
野比2007-09-26 21:02
以下是引用andrewpoon在2007-9-24 14:59:02的发言:
1.对于int型数据,若是负数,第0位为1;正数则为0.那么32位机上范围是-2,147,483,648~~2,147,483,647.
问题一:为什么负数比正数多一个呢?
问题二:最小负数-2,147,483,648转换成而二进制是怎么样的呢?
问题三:那int 型数据二进制数字 1000 0000 0000 0000 0000 0000 0000 0000转换成十进制是多少呢?

那个是第31位好不好(bit31~bit0,32bits)。。最高位~~
#15
duffebear2007-09-26 21:32
数字在计算机里面是以补码表示的啊,自己看看 微机原理或是汇编
1