One-step-one-step write algorithm (large number calculation)

Source: Internet
Author: User

Original: One-step-one-step write algorithm (large number calculation)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


We know that on the x86 32-bit CPU, int represents 32 bits, and if it is accounted for as an integer, it is about more than 4 billion. Similarly, if you are on a 64-bit CPU, the largest integer that can be represented is the 64-bit binary, which represents a much larger number. So what if you want to represent a large integer in 32 digits? It's only on our own.

First, let's look back at how the addition and subtraction of our hand-counted integers, the multiplication method, is done:

(1) Remember the multiplication formula between 9*9

(2) Remember the addition and subtraction between single and single digits

(3) All multiplication is represented by addition and left shift, and all subtraction is expressed by subtraction and right shift

After understanding the above, we can manually write the addition of a large integer:

int* big_int_add (int src1[], int length1, int src2[], int length2) {int* dest = null;int length;int index;int smaller;int p Refix = 0;if (NULL = = Src1 | | 0 >= LENGTH1 | | NULL = = Src2 | | 0 >= length2) return null;length = length1 > length2?  (Length1 + 1): (length2 + 1);d est = (int*) malloc (sizeof (int) * length); assert (NULL! = dest); memset (dest, 0, sizeof (int) * length); smaller = (Length2 < length1)? Length2:length1;for (index = 0; index < smaller; index + +) Dest[index] = Src1[index] + src2[index];if (length1 > Leng Th2) {for (; index < length1; index++) Dest[index] = Src1[index];} Else{for (; index < length2; index++) Dest[index] = Src2[index];} for (index = 0; index < length; index + +) {Dest[index] + = prefix; prefix = dest[index]/10;dest[index]%= 10;} return dest;}
The most important feature of the algorithm above is: When the calculation is not considered 10 binary, wait until all the results come out after the start of each of the binary processing.


Discuss:

After seeing the algorithm above, you can consider:

(1) How should subtraction be written?

(2) How about multiplication? What about division?


One-step-one-step write algorithm (large number calculation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.