操作系统:ubuntu11.10
在编程中,需要对边界值进行判断,程序的异常很多是对边界值的忽略,
而且这种异常往往很难发现,增加了开发,调试的难度。
加法:
-
#include <stdio.h>
-
-
//type : char,short,int,long; but not:float,double
-
typedef char type;
-
-
//success ,return 0; error ,return error_num(-1,...)
-
int add(type a, type b,type *result)
-
{
-
if(result == NULL) return -1;
-
*result = a+b;
-
-
// printf("result : %d\n",*result);
-
return (-(((a>=0) && (b>=0) && (*result < 0)) || \
-
((a<0) && (b<0) && (*result >= 0))));
-
}
-
-
int main(void)
-
{
-
type a = 80;
-
type b = 100;
-
type result = 0;
-
type ret= 0;
-
-
ret = add(a,b,&result); //(80,100,-76)
-
printf("result : %d\t + %d\t = %d\t,%d\t\n",a,b,result,ret);
-
-
ret = add(-a,b,&result); //(-80,100,20)
-
printf("result : %d\t + %d\t = %d\t,%d\t\n",-a,b,result,ret);
-
-
ret = add(a,-b,&result); //(80,-100,-20)
-
printf("result : %d\t + %d\t = %d\t,%d\t\n",a,-b,result,ret);
-
-
ret = add(-a,-b,&result); //(-80,-100,76)
-
printf("result : %d\t + %d\t = %d\t,%d\t\n",-a,-b,result,ret);
-
-
return 0;
-
}
结果:
解说:
当前 type = char
add(a,b,&result); //(80,100,-76)
a = 80 = 0101 0000
b = 100 = 0110 0100
result = 1011 0100
数据是以补码方式存放在内存中,
result 内存空间保存的值 1011 0100 是结果的补码,则:
(result)源码 = (~(result - 1)) = (~(1011 0011)) = (1100 1100) = -76
减法:
-
#include <stdio.h>
-
-
//type : char,short,int,long; but not:float,double
-
typedef char type;
-
-
//success ,return 0; error ,return error_num(-1,...)
-
int add(type a, type b,type *result)
-
{
-
if(result == NULL) return -1;
-
*result = a+b;
-
-
// printf("result : %d\n",*result);
-
return (-(((a>0) && (b>0) && (*result < 0)) || \
-
((a<0) && (b<0) && (*result > 0))));
-
}
-
-
int sub(type a,type b,type *result)
-
{
-
if(b == (type)(~b + 1))
-
{//判断b 是不是机器可以表示的最小负整数。此式可以适用于32位及64位机器。
-
-
printf("a - b = %d\n",(type)(a - b));
-
return (-(a <= 0));
-
/* 注意: 此处要有=,因为:如果不加=,
-
当0 - (-2147483648)=2147483648在32位机中是无法表示的,
-
得出的结果仍然是-2147483648,这显然是不对的。
-
*/
-
}
-
else
-
{
-
return add(a,-b,result);
-
}
-
}
-
-
int main(void)
-
{
-
type a = -30;
-
type b = -120;
-
type result = 0;
-
type ret= 0;
-
-
ret = sub(a,b,&result); //(-30,-120,90)
-
printf("result : %d\t - %d\t = %d\t,%d\t\n",a,b,result,ret);
-
-
ret = sub(-a,b,&result); //(30,-120,-106)
-
printf("result : %d\t - %d\t = %d\t,%d\t\n",-a,b,result,ret);
-
-
ret = sub(a,-b,&result); //(-30,120,106)
-
printf("result : %d\t - %d\t = %d\t,%d\t\n",a,-b,result,ret);
-
-
ret = sub(-a,-b,&result); //(-30,-120,-90)
-
printf("result : %d\t - %d\t = %d\t,%d\t\n",-a,-b,result,ret);
-
-
return 0;
-
}
结果:
解说:
当前 type = char
1,sub(-a,b,&result); //(30,-120,-106)
-a - b = result;
(-a) = 30 = 0001 1110
(-b) = 120 = 0111 1000
(-a) + (-b) = result
result = 0001 1110
+ 0111 1000
-------------------
1001 0110
result的内存空间中保存的值为 1001 0110,由于 result 是 char 类型,
即 result 中保存的值 1001 0110 是计算所得的需求的值的补码(内存中数据都是以 补码 方式保存)。
(result)源码 = (~(result - 1)) = (~(1001 0110 - 1)) = (~(1001 0101)) = (1110 1010) = -106
2,sub(a,-b,&result); //(-30,120,106)
a - (-b) = result;
(a)源码 = -30 = 1001 1110
(a)补码 = (~((a)源码) + 1) = (~(1001 1110) + 1) = (1110 0001 + 1) = (1110 0010)
则系统分配给 a 的内存中保存的数值为 1110 0010
(-(-b))源码 = (b)源码 = -120 = 1111 1000
(b)补码
= (~((b)源码) + 1) = (~(1111 1000) + 1) = (1000 0111 + 1) = (1000 1000)
则系统分配给 b 的内存中保存的数值为 1000 1000
(a) + (-(-b)) = result
result = 1110 0010
+ 1000 1000
-------------------
0110 1010
result的内存空间中保存的值为 0110 1010,由于result值的最高位为 0,表示这个数为正数,
(value)源码 = (value)补码 = result = 0110 1010 = 106
乘法:
-
#include <stdio.h>
-
-
//type : char,short,int,long; but not:float,double
-
typedef char type;
-
-
//success ,return 0; error ,return error_num(-1,...)
-
int mut(type a, type b,type *result)
-
{
-
if(result == NULL) return -1;
-
-
if((a == 0) || (b == 0))
-
{
-
*result = 0;
-
return 0;
-
}
-
-
*result = a*b;
-
-
return (-(((*result)/a) != b));
-
}
-
-
-
int main(void)
-
{
-
type a = 23;
-
type b = 121;
-
type result = 0;
-
type ret= 0;
-
-
ret = mut(a,3,&result); //(23,3,)
-
printf("result : %d\t * %d\t = %d\t,%d\t\n",a,3,result,ret);
-
-
ret = mut(0,b,&result); //(0,121,)
-
printf("result : %d\t * %d\t = %d\t,%d\t\n",0,b,result,ret);
-
-
ret = mut(a,0,&result); //(23,0,)
-
printf("result : %d\t * %d\t = %d\t,%d\t\n",a,0,result,ret);
-
-
ret = mut(0,0,&result); //(0,0,)
-
printf("result : %d\t * %d\t = %d\t,%d\t\n",0,0,result,ret);
-
-
ret = mut(a,b,&result); //(23,121,)
-
printf("result : %d\t * %d\t = %d\t,%d\t\n",a,b,result,ret);
-
-
return 0;
-
}
结果:
解说:
当前 type = char
1,mut(a,b,&result); //(23,121,-33)
a * b = result;
a = 23 = 0001 0111
b = 121 = 0111 1001
b = 0100 0000 + 0010 0000 + 0001 0000 + 0000 1000 + 0000 0001 = 2^6 + 2^5 + 2^4 + 2^3 + 2^0;
result = 0001 0111 <<6 --> 0000 0101
1100 0000 --> 1100 0000
0001 0111 <<5
--> 0000 0010 1110 0000 --> 1110 0000
0001 0111 <<4 --> 0000 0001 0111 0000 --> 0111 0000
0001 0111 <<3 --> 0000 0000 1011 1000 --> 1011 1000
+ 0001 0111 <<0 --> 0001 0111 --> 0001 0111
------------------------------------------------------------------------------------------
1101 1111
result的内存空间中保存的值为 1101 1111,由于 result 是 char 类型,由于result值的最高位为 1,表示这个数为负数,
(value)补码 =
(~((value)源码) + 1) = result = 1101 1111
(value)源码 = (~(
(value)补码 -1)) = (
~(1101 1111 -1))
= (~(1101 1110)) = 1010 0001 = -33
无符号:
-
#include <stdio.h>
-
-
//type : char,short,int,long; but not:float,double
-
typedef unsigned char type;
-
-
//success ,return 0; error ,return error_num(-1,...)
-
int add(type a, type b,type *result)
-
{
-
if(result == NULL) return -1;
-
*result = a+b;
-
-
// printf("result : %d\n",*result);
-
return (-((*result < a) || (*result < b)));
-
}
阅读(1011) | 评论(0) | 转发(0) |