鱼C论坛

 找回密码
 立即注册
查看: 360|回复: 0

[技术交流] 用bitset写了一个高精度程序(待完善)

[复制链接]
发表于 2023-10-12 00:16:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
就是模拟计算机的操作来进行运算。

打算做一个1024bit的虚拟操作系统。但是我不想用压位高精,这样就不像计算机了。我还是想用计算机最原始的运算方法来写。所以不要说这个代码常数怎么这么大。

代码如下。到时候慢慢完善。

目前做的是正整数的,应该没问题了,欢迎大家来hack.,但是使用补码应该可以很容易实现整数,浮点数的运算。

但是我现在没有时间做了。以后慢慢来。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <typeinfo>
  4. #include <bitset>
  5. #include <string>
  6. #include <utility>
  7. using namespace std;

  8. const int _bits=8;

  9. // 准备写一个高精度类。用 std::bitset

  10. inline int highest_true(bitset<_bits> x)
  11. {
  12.         for(int i=_bits-1;i>=0;i--)
  13.         {
  14.                 if(x[i])return i;
  15.         }
  16.         return -1;
  17. }

  18. inline bool operator>(bitset<_bits> x,bitset<_bits> y)
  19. {
  20.         for(int i=_bits-1;i>=0;i--)
  21.         {
  22.                 if(x[i]&&!y[i])
  23.                 return true;
  24.                 if(!x[i]&&y[i])
  25.                 return false;
  26.         }
  27.         return false;
  28. }

  29. inline bool operator<(bitset<_bits> x,bitset<_bits> y)
  30. {
  31.         for(int i=_bits-1;i>=0;i--)
  32.         {
  33.                 if(x[i]&&!y[i])
  34.                 return false;
  35.                 if(!x[i]&&y[i])
  36.                 return true;
  37.         }
  38.         return false;
  39. }

  40. inline bool operator>=(bitset<_bits> x,bitset<_bits> y)
  41. {
  42.         return !(x<y);
  43. }

  44. inline bool operator<=(bitset<_bits> x,bitset<_bits> y)
  45. {
  46.         return !(x>y);
  47. }

  48. inline bitset<_bits> operator+(bitset<_bits> x,bitset<_bits> y)
  49. {
  50.         bool c;
  51.         bitset<_bits> ans;
  52.         ans[0]=x[0]^y[0];
  53.         c=x[0]&&y[0];
  54.         for(int i=1;i<_bits;i++)
  55.         {
  56.                 ans[i]=(x[i]&&y[i]&&c)||(x[i]&&!y[i]&&!c)||(!x[i]&&y[i]&&!c)||(!x[i]&&!y[i]&&c);
  57.                 c=(x[i]&&y[i]&&c)||(x[i]&&y[i]&&!c)||(!x[i]&&y[i]&&c)||(x[i]&&!y[i]&&c);
  58.         }
  59.         return ans;
  60. }

  61. inline bitset<_bits> operator-(bitset<_bits> x,bitset<_bits> y)
  62. {
  63.         return x+~y+(bitset<_bits>)1;
  64. }

  65. inline bitset<_bits> operator*(bitset<_bits> x,bitset<_bits> y)
  66. {
  67.         bitset<_bits> tmpx=x,tmpy=y,ans;
  68.         while(tmpx.any())
  69.         {
  70.                 if(tmpx[0])
  71.                 {
  72.                         ans=ans+tmpy;
  73.                 }
  74.                 tmpx>>=1;
  75.                 tmpy<<=1;
  76.         }
  77.         return ans;
  78. }

  79. inline bitset<_bits> operator/(bitset<_bits> x,bitset<_bits> y)
  80. {
  81.         int ht1=highest_true(x),ht2=highest_true(y);
  82.         if(ht1<ht2)return (bitset<_bits>)0;
  83.         bitset<_bits> tmpx=x,tmpy=y<<(ht1-ht2),ans;
  84.         while(tmpx>=y)
  85.         {
  86.                 ans[0]=tmpx>=tmpy;
  87.                 if(ans[0])
  88.                 tmpx=tmpx-tmpy;
  89.                
  90.                 ans<<=1;
  91.                 tmpy>>=1;
  92.         }
  93.         ans<<=(highest_true(tmpy)-ht2);
  94.         return ans;
  95.        
  96. }

  97. inline pair< bitset<_bits> , bitset<_bits> > divmod(bitset<_bits> x,bitset<_bits> y)
  98. {
  99.         int ht1=highest_true(x),ht2=highest_true(y);
  100.         pair< bitset<_bits>,bitset<_bits> > ans_pair;
  101.         if(ht1<ht2)
  102.         {
  103.                 ans_pair.first=0;
  104.                 ans_pair.second=x;
  105.                 return ans_pair;
  106.         }
  107.         bitset<_bits> tmpx=x,tmpy=y<<(ht1-ht2),div;
  108.         while(1)
  109.         {
  110.                 cout<<tmpx<<endl<<tmpy<<endl<<div<<endl<<endl;
  111.                
  112.                 div[0]=(tmpx>=tmpy);
  113.                
  114.                
  115.                 if(div[0])
  116.                 tmpx=tmpx-tmpy;
  117.                 if(tmpx<y)
  118.                 break;
  119.                 div<<=1;
  120.                 tmpy>>=1;
  121.         }
  122.         div<<=(highest_true(tmpy)-ht2);
  123.         cout<<highest_true(tmpy)<<' '<<ht2<<endl;
  124.         ans_pair.first=div;
  125.         ans_pair.second=tmpx;
  126.         cout<<endl<<"div: "<<div<<" "<<(highest_true(tmpy)-ht2)<<endl;
  127.         return ans_pair;
  128. }

  129. inline void in_int(bitset<_bits> &x)
  130. {
  131.         int a;
  132.         cin>>a;
  133.         x=a;
  134. }

  135. inline void out_int(bitset<_bits> &x)
  136. {
  137.         string str=x.to_string();
  138.        
  139.         int ans=0,l=str.length();
  140.         for(int i=l-1,j=1;i>=0;i--,j*=2)
  141.         {
  142.                 if(str[i]=='1')ans+=j;
  143.         }
  144.         cout<<"output : "<<ans<<endl;
  145. }

  146. int main()
  147. {
  148.        
  149. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 16:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表