c語言-大數乘法(16進位)

來源:互聯網
上載者:User

標籤:unsigned   數組   bsp   加法   資料   http   def   下標   變數   

由來

由於工作中用到,自己在網上找了一些大數乘法源碼,都沒有符合我要求的。所以自己實現了一個。

分析過程

仿照我們小學學過的10進位乘法算式,來實現的大數乘法。 
先舉一個例子:234 * 567 


 

 

這是我們最普通的乘法算式,對於程式實現起來還有點難,因此我們再繼續計算分析: 
註:圖中的小數字,不是次方,只是進位的數) 


 


這個計算過程跟的區別就是加法跟乘法分開來計算,我在做乘法部分的時候,只是單純的計算乘法,沒有做加法,把進位的數標記出來,最後把在同一列的數字統統加起來,該進位的進位,最後起算出來結果。 
寫到這裡大概就清晰了,我們先列個數組。

 

1 //用虛擬碼數組表示數:234,5672 a[3] = {2, 3, 4}3 b[3] = {5, 6, 7}

 

為數組中表示形式 


 

 

程式講解

以上講解為分析過程,我用我們常用的10進位作為分析,在下面源碼中用到了0x100進位,其實原理是一樣的,一個unsigned char類型變數,正好最大可以支援到0xFF。

程式源碼

以下是大數乘法編譯過得程式

 1 #include <stdio.h>   2  3 typedef unsigned char u8; 4 typedef unsigned int u32; 5  6 #define N 32 7 #define CARRYNUM 0x100 8  9 void BigMul(u8* res_high, u8* res_low, u8 *a, u8 *b)10 {11     int ai, bi, ri;12     u32 tmp;13     u32 res[N*2] = {0};14     u8 c[N*2] = {0};15 16     //乘法計算核心部分17     for(ai = N-1; ai >= 0; ai--)    //a從後往前,低位往高位乘起來18     {19         for(bi = N-1; bi >=0; bi--)    //b從後往前,低位往高位乘起來     20         {21             tmp = a[ai] * b[bi];22             res[ai+bi+1] += (tmp % CARRYNUM); //取低位,並累加所有相同下標    23             res[ai+bi] += (tmp / CARRYNUM);   //取高位,並累加所有相同下標24         }25     }26     for(ri = (2*N)-1; ri > 0; ri--)27     {28         if(res[ri] > CARRYNUM-1)    //從res低位開始判斷,若大於0x100,向高位進位29         {30             res[ri-1] += (res[ri]/CARRYNUM);    //低位 進位元加到 高位31             res[ri] = res[ri]%CARRYNUM;         //低位元的進位去掉,保留低位元的低位32         }33     }34 35     //輸出36     for(ri = 0; ri < 2*N; ri++)37     {38         if(ri < N)39             res_high[ri] = res[ri];40         else41             res_low[ri-N] = res[ri];42     }43     return;44 }45 46 void print_num(u8* res_high, u8* res_low)47 {48     int i = 0;49     for(i = 0; i < N; i++)50     {51         printf("%02x ", res_high[i]); 52     }53 54     for(i = 0; i < N; i++)55     {56         printf("%02x ", res_low[i]); 57     }58 }59 60 void main()  61 {  62     //數組[0]資料高位, [31]資料低位63     //u8 a[N] = {0xDE,0x2A,0x7C,0xF8,0x48,0x24,0xCF,0xA0,0xCE,0xD7,0x16,0xAD,0xF5,0xD4,0x7D,0xE9,0x3D,0xFA,0x31,0xEB,0x69,0x26,0xB7,0xF7,0xDF,0x69,0xBD,0xE0,0x1F,0x9E,0xDB,0x0D};64     //u8 b[N] = {0x5A,0x1C,0x23,0x99,0x87,0x70,0x82,0xB8,0x12,0x50,0xCB,0x2A,0x7F,0x8B,0x74,0x53,0x5D,0x38,0x05,0xB5,0xB0,0xFE,0x8C,0x0E,0xF1,0xE7,0x73,0x15,0x47,0xD4,0x9A,0xA2};65 66     //u8 a[N] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};67     //u8 b[N] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};68 69     //u8 a[N] = {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11};70     //u8 b[N] = {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11};71 72     //u8 a[N] = {0x00,0x00,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11};73     //u8 b[N] = {0x00,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11};74 75     u8 a[N] = {0x00,0x00,0x00,0xD1,0xB8,0xAA,0x3E,0x1B,0x32,0xDB,0xB9,0x5A,0x13,0xB3,0x52,0x79,0x10,0x22,0x1E,0x47,0x49,0x00,0x8D,0xF9,0xA3,0x40,0xE6,0x9D,0x68,0x38,0x2C,0x58};76     u8 b[N] = {0x00,0x00,0x00,0x00,0x00,0xBF,0xB7,0x4C,0x0C,0xBF,0x09,0x13,0x1E,0x1E,0xEC,0x72,0x21,0x60,0xE5,0x5F,0xAD,0xCF,0x8E,0x50,0x50,0xE7,0xBA,0xF5,0xE9,0x6B,0x3D,0x5E};77 78 79     u8 res_high[N] = {0};80     u8 res_low[N] = {0};81     int i = 0;82 83     BigMul(res_high, res_low, a, b); 84 85     print_num(res_high, res_low);86 87 }  

 

以下是執行結果:

00 00 00 00 00 00 00 00 9d 0e f0 53 4c 63 04 72 6f 67 38 02 90 dd a1 d9 3a 25 0d df d2 4a d4 b4 7b 5a 3a 1c e7 9e db 99 6e 16 0d 00 2e b8 06 be bd 5c c0 38 37 8a 41 ff 21 d0 3f b0 47 f9 40 50 請按任意鍵繼續…

代碼打包:▇▆▅▃▂▁here▁▂▃▅▆▇

c語言-大數乘法(16進位)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.