zoukankan      html  css  js  c++  java
  • 多种方案 测试 有无符号数包括 不适用大小于符号判断符号数

    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

  • 相关阅读:
    TCP 重置攻击的工作原理
    Openshift 4.4 静态 IP 离线安装系列:初始安装
    跟我学SpringCloud | 终篇:文章汇总(持续更新)
    老司机带你玩转面试(4):Redis 高可用之哨兵模式
    老司机带你玩转面试(3):Redis 高可用之主从模式
    老司机带你玩转面试(2):Redis 过期策略以及缓存雪崩、击穿、穿透
    Python 图像处理 OpenCV (14):图像金字塔
    老司机带你玩转面试(1):缓存中间件 Redis 基础知识以及数据持久化
    Python 图像处理 OpenCV (13): Scharr 算子和 LOG 算子边缘检测技术
    Python 图像处理 OpenCV (12): Roberts 算子、 Prewitt 算子、 Sobel 算子和 Laplacian 算子边缘检测技术
  • 原文地址:https://www.cnblogs.com/titer1/p/2425724.html
Copyright © 2011-2022 走看看