如何求int型数据的最大值?

lejingxin 2008-03-26 10:07:08
我用下面的程序算出了INT型数据的最大值为2147483647,但是运算速度太慢了有没有更好的算法,谢谢?

#include<stdio.h>
#include<string.h>
void main()
{
int a=1,bj=1;
while(bj)
{
a++;
if(a<0)
bj=0;
}
printf("%d\n",a-1);
}
...全文
2769 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
power_devil 2009-07-22
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 matrixdwy 的回复:]
~(0 000 0000) -> 1 111 1111 (应该是十进制的-127)
而事实上是1 000 0001
[/Quote]
“因此我怀疑在对有符号整型进行取反操作的时候,系统实际上是把除符号位意外的位统统保留原样,再最低位补1。”
楼上的老兄,你想岔了吧~ 你的怀疑是不成立的!
按位取反操作就是实实在在地把每一位都取反!
就拿~0来说吧,0000 0000取反后为1111 1111,计算机的确是按照以上规则来计算的,为什么结果却是1000 0001呢?原因如下:1000 0001和1111 1111的关系叫做“补码”,当数值 1111 1111 存储在系统中时,计算机会认为这是一个负数(最高位符号位为1),因此将其输出时,计算机会按照补码的规则将其再做一次补码转换,即还原成原码 1000 0001(计算机认为的原码)并转换成十进制的-1输出,而并非直接将 1111 1111转换成十进制的-127输出(如果是正数则是直接转换)!

补码是计算机内部的数据存储的表示方式,不论是补码的运算规则还是补码与十进制数的换算规则都与通常的规则有所不同!因此将补码当成我们通常的二进制数并加以转换为十进制是极其错误的!
补码的转换规则是:正数的补码为它本身,负数的补码是符号位不变,其它位取反加一。计算机内部所有数都是用补码表示,这样可以将符号位和其它位统一处理,同时,还可以将减法(也就是加上一个负数)转换为加法来处理。
i.e. 3-5=-2 =》3+(-5)=2 =》0000 0011 + 1000 0101(-5的原码)=》0000 0011 + 1111 1011(-5的补码)= 1111 1110(-2的补码)=》1000 0010(-2的原码)
matrixdwy 2008-03-28
  • 打赏
  • 举报
回复
我带上一个求int型最小取值的代码


#include<iostream.h>
void main()
{
min=(unsigned int)(~1)>>1;
min=~min;
cout<<min<<endl;
/*
关于如何求得int的最小值:
同样假设有符号int型在系统中占用8位二进制码。

本来,如果把0(有符号)取反,0 000 0000->1 111 1111,就可以直接求出int的最小取值了。但是有趣的是,事实并非如此:
~0的结果是-1
~1的结果是-2
~2的结果是-3
~3的结果是-4
为什么会是这样?那我们把以上参与的数字统统变成二进制码来看一下。
~(0 000 0000) -> 1 111 1111 (应该是十进制的-127)
而事实上是1 000 0001

~(0 000 0001) -> 1 111 1110 (应该是十进制的-126)
而事实上是1 000 0010

~(0 000 0010) -> 1 111 1101 (应该是十进制的-125)
而事实上是1 000 0011

~(0 000 0011) -> 1 111 1100 (应该是十进制的-124)
而事实上是1 000 0100

因此我怀疑在对有符号整型进行取反操作的时候,系统实际上是把除符号位意外的位统统保留原样,再最低位补1,即:
~0 000 0000 -> 1 000 0000 ->(最低位补1) 1 000 0001
~0 000 0001 -> 1 000 0001 ->(最低位补1) 1 000 0010
~0 000 0010 -> 1 000 0010 ->(最低位补1) 1 000 0011
~0 000 0011 -> 1 000 0011 ->(最低位补1) 1 000 0100

所以在求有符号int型的最小取值的时候,我们应该先定义一个无符号1(0000 0001),对1进行取反(1111 1110),再将其右移动一位
0111 1111(高位补0,低位抛弃),然后转换成有符号(0 111 1111),再对其进行取反,(1 111 1111) -> (1 1000 0000)=-128.

但是这样就变成9位数了,因此我认为,系统在对有符号int进行取反的操作时,并没有真正对其低位补1,而是人为的规定了
1 000 0000 这个负数的起点 就是-1,而不是-0。
*/
}

visame 2008-03-27
  • 打赏
  • 举报
回复

看看C标准就啥都明白了。
5.2.4.2 Numerical limits
1 An implementation is required to document all the limits specified in this subclause,
which are specified in the headers <limits.h> and <float.h>. Additional limits are
specified in <stdint.h>.
Forward references: integer types <stdint.h> (7.18).
5.2.4.2.1 Sizes of integer types <limits.h>
1 The values given below shall be replaced by constant expressions suitable for use in #if
preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the
following shall be replaced by expressions that have the same type as would an
expression that is an object of the corresponding type converted according to the integer
promotions. Their implementation-defined values shall be equal or greater in magnitude
(absolute value) to those shown, with the same sign.
— number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
— minimum value for an object of type signed char
SCHAR_MIN -127 // -(27 - 1)//应该是2的7次方减1(不是27减一),下同
— maximum value for an object of type signed char
SCHAR_MAX +127 // 27 - 1
— maximum value for an object of type unsigned char
UCHAR_MAX 255 // 28 - 1
— minimum value for an object of type char
CHAR_MIN see below
— maximum value for an object of type char
CHAR_MAX see below
— maximum number of bytes in a multibyte character, for any supported locale
MB_LEN_MAX 1
— minimum value for an object of type short int
SHRT_MIN -32767 // -(215 - 1)
— maximum value for an object of type short int
SHRT_MAX +32767 // 215 - 1
— maximum value for an object of type unsigned short int
USHRT_MAX 65535 // 216 - 1
— minimum value for an object of type int
INT_MIN -32767 // -(215 - 1)
— maximum value for an object of type int
INT_MAX +32767 // 215 - 1
— maximum value for an object of type unsigned int
UINT_MAX 65535 // 216 - 1
— minimum value for an object of type long int
LONG_MIN -2147483647 // -(231 - 1)
— maximum value for an object of type long int
LONG_MAX +2147483647 // 231 - 1
— maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 232 - 1
— minimum value for an object of type long long int
LLONG_MIN -9223372036854775807 // -(263 - 1)
— maximum value for an object of type long long int
LLONG_MAX +9223372036854775807 // 263 - 1
— maximum value for an object of type unsigned long long int
ULLONG_MAX 18446744073709551615 // 264 - 1
下面是C++,C++中有一个专门的类叫做numeric_limits。
18.2.1.2 numeric_limits members [lib.numeric.limits.members]
static T min() throw();
1 Minimum finite value.181)
2 For floating types with denormalization, returns the minimum positive normalized value.
3 Meaningful for all specializations in which is_bounded != false, or is_bounded == false
&& is_signed == false.
static T max() throw();
4 Maximum finite value.182)
5 Meaningful for all specializations in which is_bounded != false.
guangling_san 2008-03-27
  • 打赏
  • 举报
回复
/*
不知道楼主用的什么编译器?书上说int型的是-32768~32767的取值范围。楼主所说的数值是long int型的取值范围吧?我用win-tc编译器,LZ的程序值为32767。
后我自己作了个程序,int型取值一样,不过改为long型后,却得值为-1。这是为什么呢?希望各位帮忙解答一下,不胜感激。
*/

#include <stdio.h>
int main(void)
{

int a;
for(a=30000;a>0;a++);
printf("%d\n",a-1);

getch();
}
ttkk_2007 2008-03-27
  • 打赏
  • 举报
回复
int x = 0x7fffffff;
printf("%d\n", x);
wbczyh 2008-03-27
  • 打赏
  • 举报
回复
假如int是16位的话就是上面的结果,32位类似。
wbczyh 2008-03-27
  • 打赏
  • 举报
回复
~ 0000 0000 0000 0000 = 1111 1111 1111 1111
1111 1111 1111 1111 >> 1 = 0111 1111 1111 1111 = +32767
wbczyh 2008-03-27
  • 打赏
  • 举报
回复
int(((unsigned int)(~0))>>1)这个方法好,呵呵
houdongfeng 2008-03-27
  • 打赏
  • 举报
回复
#include <limits.h>

printf("%d\n", INT_MAX);

int(((unsigned int)(~0))>>1)
ttkk_2007 2008-03-27
  • 打赏
  • 举报
回复
32位的又如何
lejingxin 2008-03-27
  • 打赏
  • 举报
回复
我用的是vc++6.0是32位的!
arong1234 2008-03-26
  • 打赏
  • 举报
回复
~0把所有位变为1,转换乘无符号数,确保右移是逻辑右移(还是其他名字?:)),也就是高位会因为右移空出个0,这样就作出一个最高位(符号位)为0其他位都为1的整数了
bm1408 2008-03-26
  • 打赏
  • 举报
回复
int(((unsigned int)(~0))>>1)

我看了半天,看来我需要补充一下知识了
arong1234 2008-03-26
  • 打赏
  • 举报
回复
最大值就是符号位为0,其他位都为1的整数,哪要那么复杂,不就是
int(((unsigned int)(~0))>>1)
rover___ 2008-03-26
  • 打赏
  • 举报
回复
还可以用sizeof(type)来求。知道类型的存储字节数,就很明白了。
lejingxin 2008-03-26
  • 打赏
  • 举报
回复
谢谢!
其他类型的数据最大值是不是也是这样的求的呢?
sheenl 2008-03-26
  • 打赏
  • 举报
回复
c语言就是
#include <limits.h>

printf("%d\n", INT_MAX);
sheenl 2008-03-26
  • 打赏
  • 举报
回复
cout<<numeric_limits<int>::max()<<endl;

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧