128-bit整数定义

 

//头文件 
#define   Unit__int128H 
#include    <iostream.h>  
class   __int128 
{//实现的是无符号的__int128 
private: 
             unsigned   long   val[4]; 
int HexToInt(char hex) 
             { 
                         if(hex   > =    '0 '&&hex    <= '9 ')   return   hex   -    '0 '; 
                         else   if(hex   > =    'A '&&hex    <= 'F ')   return   hex   -    'A '   +   10; 
                         else   if(hex   > =    'a '&&hex    <= 'f ')   return   hex   -    'a '   +   10; 
                         else   return   0; 
             } 
public: 
             ~__int128(){} 
             __int128(){val[0]=val[1]=val[2]=val[3]=0;} 
             __int128(const   char   *   stval); 
             __int128(const   long   &   rval); 
             __int128(const   __int128   &   rval); 

             __int128   &   operator=   (const   __int128   &   rval); 
             __int128   &   operator=   (const   char   *   stval); 

             __int128   operator+   (const   __int128   &   rval); 
             __int128   operator+   (const   long   &   rval); 

             __int128   operator-   (const   __int128   &   rval); 
             __int128   operator-   (const   long   &   rval); 

             __int128   operator^   (const   __int128   &   rval); 
             __int128   operator|   (const   __int128   &   rval); 
             __int128   operator&   (const   __int128   &   rval); 
             __int128   operator> > (const   long   &   rval); 
             __int128   operator < <(const   long   &   rval); 

             friend   __int128   operator~(const   __int128   &   rval); 
             friend   ostream   &   operator    < <   (ostream   &   os,__int128   &   rval); 
             friend   istream   &   operator   > >    (ostream   &   is,__int128   &   rval); 

}; 


#endif 
//

#include    "Unit__int128.h " 

__int128::__int128(const   __int128   &   rval) 
{ 
             for(int   i   =   0;   i    <   4;   i++) 
             { 
                         val[i]   =   rval.val[i]; 
             } 
} 
__int128::__int128(const   long   &   rval) 
{ 
             val[0]   =   rval; 
} 
__int128::__int128(const   char   *   stval) 
{ 
             *this   =   stval; 
} 
__int128   &   __int128::operator=   (const   __int128   &   rval) 
{ 
             if(this   ==   &rval)   return   *this; 
             val[0]   =   rval.val[0]; 
             val[1]   =   rval.val[1]; 
             val[2]   =   rval.val[2]; 
             val[3]   =   rval.val[3]; 
             return   *this; 
} 
__int128   &   __int128::operator=   (const   char   *   stval) 
{ 
    //只作对十六进制数的支持,因为对十进制只要转化即可支持, 
  //val[0],val[1],val[2],val[3]分别代表着从低位到高位的值。 

           val[0]=val[1]=val[2]=val[3]=0; 
             if(stval   ==   NULL   ||   stval[0]   ==   0)   return   *this; 
             if(stval[0]   !=    '0 '   &&   (stval[1]!=    'x '||stval[1]   !=    'X '))   return   *this; 

             char   buf[33]; 
             memset(buf, '0 ',32); 
             buf[32]   =   0; 

             long   len   =   strlen(stval); 
             len   -=2; 
             strncpy(buf+32-len,stval+2,len); 

             for(int   i   =   0;   i    <   4;   i++) 
             { 
                         for(int   j   =   0;   j    <   8;   j++) 
                         { 
                                     val[3-i]   +=   HexToInt(buf[i*8+j]); 
                                     if(j   ==   7)   break;//最后一位加上来后,因为十六进制个位,所以不能移位 
                                   //应该退出循环。 
                                   val[3-i]    < <=   4; 
                         } 
             } 
             return   *this; 
} 
__int128   __int128::operator+(const   __int128   &   rval) 
{ 
             unsigned   long   a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4; 

             a1=val[0];                  a2=val[1];                  a3=val[2];                  a4=val[3]; 
             b1=rval.val[0];   b2=rval.val[1];   b3=rval.val[2];   b4=rval.val[3]; 
             c1   =   c2   =   c3   =   c4   =   0; 
             __asm 
             { 
                         push   eax; 
                         mov   eax,dword   ptr   a1; 
                         add   eax,dword   ptr   b1; 
                         mov   dword   ptr   c1,eax; 
                         mov   eax,dword   ptr   a2; 
                         adc   eax,dword   ptr   b2; 
                         mov   dword   ptr   c2,eax; 
                         mov   eax,dword   ptr   a3; 
                         adc   eax,dword   ptr   b3; 
                         mov   dword   ptr   c3,eax; 
                         mov   eax,dword   ptr   a4; 
                         adc   eax,dword   ptr   b4; 
                         mov   dword   ptr   c4,eax; 
                         pop   eax; 
             } 
             __int128   tmp; 
             tmp.val[0]   =   c1;tmp.val[1]   =   c2; 
             tmp.val[2]   =   c3;tmp.val[3]   =   c4; 
             return   tmp; 
} 
__int128   __int128::operator+(const   long   &   rval) 
{ 
             return   *this+__int128(rval);             
} 
__int128   __int128::operator-(const   __int128   &   rval) 
{ 
             unsigned   long   a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4; 

             a1=val[0];                  a2=val[1];                  a3=val[2];                  a4=val[3]; 
             b1=rval.val[0];   b2=rval.val[1];   b3=rval.val[2];   b4=rval.val[3]; 
             c1   =   c2   =   c3   =   c4   =   0; 
             __asm 
             { 
                         push   eax; 
                         mov   eax,dword   ptr   a1; 
                         sub   eax,dword   ptr   b1; 
                         mov   dword   ptr   c1,eax; 
                         mov   eax,dword   ptr   a2; 
                         sbb   eax,dword   ptr   b2; 
                         mov   dword   ptr   c2,eax; 
                         mov   eax,dword   ptr   a3; 
                         sbb   eax,dword   ptr   b3; 
                         mov   dword   ptr   c3,eax; 
                         mov   eax,dword   ptr   a4; 
                         sbb   eax,dword   ptr   b4; 
                         mov   dword   ptr   c4,eax; 
                         pop   eax; 
             } 
             __int128   tmp; 
             tmp.val[0]   =   c1;tmp.val[1]   =   c2; 
             tmp.val[2]   =   c3;tmp.val[3]   =   c4; 
             return   tmp; 
} 
__int128   __int128::operator-(const   long   &   rval) 
{ 
             return   *this-__int128(rval); 
} 
__int128   __int128::operator^   (const   __int128   &   rval) 
{ 
             __int128   tmp; 
             tmp.val[0]   =   val[0]^rval.val[0]; 
             tmp.val[1]   =   val[1]^rval.val[1]; 
             tmp.val[2]   =   val[2]^rval.val[2]; 
             tmp.val[3]   =   val[3]^rval.val[3]; 
             return   tmp; 
} 
__int128   __int128::operator|   (const   __int128   &   rval) 
{ 
             __int128   tmp; 
             tmp.val[0]   =   val[0]|rval.val[0]; 
             tmp.val[1]   =   val[1]|rval.val[1]; 
             tmp.val[2]   =   val[2]|rval.val[2]; 
             tmp.val[3]   =   val[3]|rval.val[3]; 
             return   tmp; 
} 
__int128   __int128::operator&   (const   __int128   &   rval) 
{ 
             __int128   tmp; 
             tmp.val[0]   =   val[0]&rval.val[0]; 
             tmp.val[1]   =   val[1]&rval.val[1]; 
             tmp.val[2]   =   val[2]&rval.val[2]; 
             tmp.val[3]   =   val[3]&rval.val[3]; 
             return   tmp; 
} 
__int128   __int128::operator> > (const   long   &   rval) 
{ 
             if(rval   > =   128)   return   __int128(long(0)); 
             long   movbits   =   rval; 
             if(movbits   ==   0)   return   *this; 
             unsigned   long   a1,a2,a3,a4; 
             a1=val[0];                  a2=val[1];                  a3=val[2];                  a4=val[3]; 
             unsigned   long   carryval   =   0x80000000; 
             unsigned   long   tempval; 
             __asm 
             { 
                         push   eax 
                         push   ebx 
                         push   ecx 
                         mov   ecx,dword   ptr   movbits 
LOOPH:      mov   eax,dword   ptr   a4 
                         shr   eax,1 
                         mov   dword   ptr   a4,eax 
                         jc   BYTE4 
                         mov   ebx,0 
BACK4:      mov   eax,dword   ptr   a3 
                         shr   eax,1 
                         push   ebx 
                         jc   BYTE3 
                         mov   ebx,0 
BACK3:      mov   dword   ptr   tempval,ebx 
                         pop   ebx; 
                         add   eax,ebx 
                         mov   dword   ptr   a3,eax 
                         mov   ebx,dword   ptr   tempval 
                         mov   eax,dword   ptr   a2 
                         shr   eax,1 
                         push   ebx 
                         jc   BYTE2 
                         mov   ebx,0 
BACK2:      mov   dword   ptr   tempval,ebx 
                         pop   ebx; 
                         add   eax,ebx 
                         mov   dword   ptr   a2,eax 
                         mov   ebx,dword   ptr   tempval 
                         mov   eax,dword   ptr   a1 
                         shr   eax,1 
                         push   ebx 
                         jc   BYTE1 
                         mov   ebx,0 
BACK1:      mov   dword   ptr   tempval,ebx 
                         pop   ebx 
                         add   eax,ebx 
                         mov   dword   ptr   a1,eax 
                         dec   ecx 
                         jnz   LOOPH 
                         jz      QUIT 
BYTE4:      mov   ebx,carryval 
                         jmp   BACK4 
BYTE3:      mov   ebx,carryval 
                         jmp   BACK3 
BYTE2:      mov   ebx,carryval 
                         jmp   BACK2 
BYTE1:      mov   ebx,carryval 
                         jmp   BACK1 
QUIT:         pop   ecx 
                         pop   ebx 
                         pop   eax 
             } 
             __int128   tmp; 
             tmp.val[0]   =   a1; 
             tmp.val[1]   =   a2; 
             tmp.val[2]   =   a3; 
             tmp.val[3]   =   a4; 
             return   tmp; 
} 
__int128   __int128::operator < <(const   long   &   rval) 
{ 
             if(rval   > =   128)   return   __int128(long(0)); 
             long   movbits   =   rval; 
             if(movbits   ==   0)   return   *this; 
             unsigned   long   a1,a2,a3,a4; 
             a1=val[0];                  a2=val[1];                  a3=val[2];                  a4=val[3]; 
             unsigned   long   carryval   =   0x00000001; 
             unsigned   long   tempval; 
             __asm 
             { 
                         push   eax 
                         push   ebx 
                         push   ecx 
                         mov   ecx,dword   ptr   movbits 
LOOPH:      mov   eax,dword   ptr   a1 
                         shl   eax,1 
                         mov   dword   ptr   a1,eax 
                         jc   BYTE4 
                         mov   ebx,0 
BACK4:      mov   eax,dword   ptr   a2 
                         shl   eax,1 
                         push   ebx 
                         jc   BYTE3 
                         mov   ebx,0 
BACK3:      mov   dword   ptr   tempval,ebx 
                         pop   ebx; 
                         add   eax,ebx 
                         mov   dword   ptr   a2,eax 
                         mov   ebx,dword   ptr   tempval 
                         mov   eax,dword   ptr   a3 
                         shl   eax,1 
                         push   ebx 
                         jc   BYTE2 
                         mov   ebx,0 
BACK2:      mov   dword   ptr   tempval,ebx 
                         pop   ebx; 
                         add   eax,ebx 
                         mov   dword   ptr   a3,eax 
                         mov   ebx,dword   ptr   tempval 
                         mov   eax,dword   ptr   a4 
                         shl   eax,1 
                         push   ebx 
                         jc   BYTE1 
                         mov   ebx,0 
BACK1:      mov   dword   ptr   tempval,ebx 
                         pop   ebx 
                         add   eax,ebx 
                         mov   dword   ptr   a4,eax 
                         dec   ecx 
                         jnz   LOOPH 
                         jz      QUIT 
BYTE4:      mov   ebx,carryval 
                         jmp   BACK4 
BYTE3:      mov   ebx,carryval 
                         jmp   BACK3 
BYTE2:      mov   ebx,carryval 
                         jmp   BACK2 
BYTE1:      mov   ebx,carryval 
                         jmp   BACK1 
QUIT:         pop   ecx 
                         pop   ebx 
                         pop   eax 
             } 
             __int128   tmp; 
             tmp.val[0]   =   a1; 
             tmp.val[1]   =   a2; 
             tmp.val[2]   =   a3; 
             tmp.val[3]   =   a4; 
             return   tmp; 
} 

__int128   operator~(const   __int128   &   rval) 
{ 
             __int128   tmp; 
             tmp.val[0]   =   ~rval.val[0]; 
             tmp.val[1]   =   ~rval.val[1]; 
             tmp.val[2]   =   ~rval.val[2]; 
             tmp.val[3]   =   ~rval.val[3]; 
             return   tmp; 
} 

ostream   &   operator    < <   (ostream   &   os,__int128   &   rval) 
{ 
             os    < < "0x " < <hex; 
             os.width(8); 
             os.fill( '0 '); 
             os    < <(unsigned   long)rval.val[3]; 
             os.width(8); 
             os    < <(unsigned   long)rval.val[2]; 
             os.width(8); 
             os    < <(unsigned   long)rval.val[1]; 
             os.width(8); 
             os    < <(unsigned   long)rval.val[0]; 
             os    < <   dec; 
             os.width(); 
             os.fill(); 
             return   os; 
} 
istream   &   operator   > >    (istream   &   is,__int128   &   rval) 
{ 
             char   *   str   =   new   char[100]; 
             is   > >    str; 
             rval   =   __int128(str); 
             delete   []   str; 
             return   is; 
}


/

测试用例: 
#include    "Unit__int128.h " 

using   namespace   std; 
void   main   (   ) 
{ 
             using   namespace   std; 
             __int128   iii( "0x1111111111111 "); 
             __int128   jjj( "0x2222222222221 "); 
             __int128   zzz; 
             zzz   =   iii+jjj; 
             cout    < <   zzz    < <endl; 
             zzz   =   zzz   > >    4; 
             cout    < <   zzz    < <endl; 
             zzz   =   zzz    < <   4; 
             cout    < <   zzz    < <endl; 
             zzz   =   iii   -   jjj; 
             cout    < <   zzz < <endl; 
             int   i; 
             cin   > >    i; 
} 
//输出结果: 
0x00000000000000000003333333333332 
0x00000000000000000000333333333333 
0x00000000000000000003333333333330 
0xfffffffffffffffffffeeeeeeeeeeef0


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值