69,372
社区成员
发帖
与我相关
我的任务
分享
#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。
*/
}
看看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.
#include <stdio.h>
int main(void)
{
int a;
for(a=30000;a>0;a++);
printf("%d\n",a-1);
getch();
}