多种方案 测试 有无符号数--包括 不适用大小于符号判断符号数

void test_符号_取补(){

//真的要设计这么一个函数,如何保证无符号传进来,还是无符号数,这个还得思考。

//能够在传参数的时候,判断有无符号?

}

 

#define MAX_UNSIGNED (  (1<<31)-1) //使用这个数字,对没有效果

//#define UNSIGNED_OR_NOT(AA) MAX_UNSIGNED+(AA)>0?1:0             //0 pass

 

#define UNSIGNED_OR_NOT(AA) MAX_UNSIGNED+1+(AA)>0?1:0 //加处理

 

#define UNSIGNED_OR_NOT(AA) MAX_UNSIGNED+1+(AA)>0?1:0 //加处理

#define UNSIGNED_OR_NOT_FINAL(AA)   (AA? (MAX_UNSIGNED+1+(AA)>0?1:0 ) : 0  )//多加括号

 

 

 

void test_各种测试符号数(){

/************************************************************************/

/*  来源c expert        

精华总结:iA>0 && ~iA>0

*/

/************************************************************************/

      // 3 solution to find signed or not

      int iA=0,iB=-1;

      unsigned int uiA=0,uiB=1;

 

    if ( iA>0 && ~iA>0   )

    {

           cout<<"signed ia"<<endl;

    }

 

      if ( iB>0 && ~iB>0     )

      {

           cout<<"signed ib"<<endl;

      }

 

      if ( uiA>0 && ~uiA>0      )

      {

           cout<<"unsigned uia"<<endl;

      }

 

      if ( uiB>0 && ~uiB>0 )

      {

           cout<<"unsigned uib"<<endl;

      }

    //小结:上面模板验证,是非符号数

      cout<<"----------"<<endl;

 

      if ( UNSIGNED_OR_NOT(iA) >0) //0+1+ max,宏设计的时候,无符号返回大于

      {

           cout<<"IA sigend"<<endl;

      }

 

      if (UNSIGNED_OR_NOT(uiA)>0) //

      {

           cout<<"uIA UNsigend"<<endl;

      }

 

      if (UNSIGNED_OR_NOT(iB) >0) // 这个就是违反的案例额-1  + 1 +max..........这是本次测试的发现,单纯使用加一个最大的不一定能成功。

      {

           cout<<"Ib sigend bug!!!"<<endl;

      }

 

      if (UNSIGNED_OR_NOT(uiB)>0)

      {

           cout<<"uIb UNsigend"<<endl;

      }

 

 

      cout<<"+++++++++++强化版本出现,不适用大于小于符号判断"<<endl;

      //思考UNSIGNED_OR_NOT_FINAL//  //这里就不用强制转换了???

 

      if (UNSIGNED_OR_NOT_FINAL(iA) >0)

      {

           cout<<"IA sigend"<<endl;

      }

 

      if (UNSIGNED_OR_NOT_FINAL(uiA)>0) //

      {

           cout<<"uIA UNsigend"<<endl;

      }

 

      if (UNSIGNED_OR_NOT_FINAL(iB) >0)

      {

           cout<<"Ib sigend"<<endl;

      }

 

      if (UNSIGNED_OR_NOT_FINAL(uiB)>0)

      {

           cout<<"uIb UNsigend"<<endl;

      }

 

      cout<<"+++++++++++"<<endl;

      /************************************************************************/

      /* /-1  + 1 +max..........这是本次测试的发现,单纯使用加一个最大的不一定能成功。     还是要用大小于符号。                                                             

      下面尝试找一种方法:对于两端的,正的+最大应该负,或者负的-正的,应该魏征。。就是说:想法设法制造负数。

      target? (target+max31+1 ? :0,1) : 0

      */

      /************************************************************************/

      cout<<"边界极限测试"<<endl;

      cout<<(1<<31)-1 <<endl;

      cout<< (unsigned int) 1+ (1<<31)-1 <<endl;

      cout<< (unsigned int) 1- (1<<31)+1 <<endl<<endl;

 

      cout<< (unsigned int) 0+ (1<<31)-1 <<endl;

      cout<< (unsigned int) 0- (1<<31)+1 <<endl<<endl;

 

      cout<< (unsigned int) (1<<32)-1 + (1<<31)-1 <<endl;//已经溢出

      cout<< (unsigned int) (1<<32)-1 - (1<<31)+1 <<endl;

 

 

      cout<< (unsigned int) (1<<31)-1 + (1<<31)-1 <<endl;//已经溢出

      cout<< (unsigned int) (1<<31)-1 - (1<<31)+1 <<endl;

 

      cout<<"有符号数边界极限测试"<<endl; //其实,宏也可以弄范围测试

      cout<<  1+ (1<<31)-1 <<endl;

      cout<<  1- (1<<31)+1 <<endl<<endl;

 

      cout<<  0+ (1<<31)-1 <<endl;

      cout<<  0- (1<<31)+1 <<endl<<endl;

 

      cout<<  (1<<32)-1 + (1<<31)-1 <<endl;//已经溢出

      cout<<  (1<<32)-1 - (1<<31)+1 <<endl;

 

 

      cout<<  (1<<31)-1 + (1<<31)-1 <<endl;//已经溢出。。。。。。想想最大加最大多少,-2...其实1<<31 两倍就是溢出了

      cout<<  (1<<31)-1 - (1<<31)+1 <<endl;

 

/************************************************************************/

/* 暂停。。有点大。                                                                    */

/************************************************************************/

}

 

unsigned uib

----------

uIA UNsigend

Ib sigend bug!!!

uIb UNsigend

+++++++++++强化版本出现,不

Ib sigend

uIb UNsigend

+++++++++++

边界 极限测试

2147483647

2147483648

2147483650

 

2147483647

2147483649

 

2147483646

2147483648

4294967294

0

有符号数 边界 极限测试

-2147483648

-2147483646

 

2147483647

-2147483647

 

2147483646

-2147483648

-2

0

 

 

转载于:https://www.cnblogs.com/titer1/archive/2012/03/30/2425724.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值