Large integer algorithm [08] signed addition and subtraction

Source: Internet
Author: User

★ Intro

The previous articles introduced comparison operations, absolute addition and absolute subtraction, which can now be used to construct a signed number subtraction algorithm.

< Span style= "Font-family:microsoft Yahei;" > ★ Signed number addition
< Span style= "Font-family:microsoft Yahei;" >           The addition of the   signed number is divided into two cases: the same number and the different number. < Span style= "Font-family:microsoft Yahei;" >           1.   If the two numbers are the same number, the absolute addition is performed, and if the two numbers are non-negative, the result is non-negative, and if the two numbers are negative, the result is also negative.            2.  If two numbers are different, absolute subtraction is performed, and the lower absolute number is reduced by the larger number of absolute values. The final result of the z symbol is determined by the absolute size of x and y: If the absolute value of x is greater than or equal to Y, then the symbol z is the same as X (Note that there may be a 0 case), otherwise the z symbol is the opposite of X.
int bn_add_bn (bignum *z, const bignum *x, const bignum *y) {    int ret;    int sign;    Sign = x->sign;    if (x->sign = = y->sign)    {        Bn_check (Bn_add_abs (z, x, y));        Z->sign = sign;    }    else    {        if (bn_cmp_abs (x, y) >= 0)        {            Bn_check (Bn_sub_abs (z, x, y));            Z->sign = sign;        }        else        {            Bn_check (Bn_sub_abs (z, y, x));            Z->sign =-sign;        }    }    if (Bn_is_zero (z))        z->sign = 1;clean:    return ret;}

Note that if the two number is different, but the absolute value is equal, then there may be a phenomenon of-0 (for example, x =-A, y = a), which is inconsistent with the previous provisions, so at the end of the sentence, if z = 0, force the sign bit to be set to 1. Bn_is_zero is a macro definition:

#define Bn_is_zero (x) ((x->used = = 0)? 1:0)

        ★ Signed number Subtraction

            is similar to the addition of signed numbers, and the symbolic number subtraction is divided into two cases:

            1.  Two number of different numbers: perform absolute addition. The symbol for the result z is determined by x, if X is non-negative, then Z is positive, and if x is negative, z is negative.

            2.  Two numbers: performs absolute subtraction, subtracting the number of absolute values by a large number of absolute values. The sign of the result z is determined by the absolute size of x and Y, and if the absolute of X is greater than or equal to the absolute value of Y, then Z and X are the same number (0 may appear), otherwise z and X are different.

int bn_sub_bn (bignum *z, const bignum *x, const bignum *y) {    int ret;    int sign;    Sign = x->sign;    if (x->sign! = y->sign)    {        Bn_check (Bn_add_abs (z, x, y));        Z->sign = sign;    }    else    {        if (bn_cmp_abs (x, y) >= 0)        {            Bn_check (Bn_sub_abs (z, x, y));            Z->sign = sign;        }        else        {            Bn_check (Bn_sub_abs (z, y, x));            Z->sign =-sign;        }    }    if (Bn_is_zero (z))        z->sign = 1;clean:    return ret;}

also, to avoid the case of 0, add a judgment to whether Z equals 0 at the end.

        ★ Single-digit addition and subtraction

the single-digit algorithm mainly calculates a large integer and a single-precision number. These two algorithms are useful in handling the addition and subtraction of small-scale data. For single-digit addition and subtraction, the default input is a large integer and a signed single-precision number, resulting in a large integer. In processing, instead of rewriting two algorithms, the single-precision number is assigned to a temporary bignum variable and then calculated using the two signed number algorithm above.

            1.  Single digit addition:

int Bn_add_int (bignum *z, const bignum *x, const bn_sint y) {    int ret;    Bignum t[1];    Bn_digit p[1];    P[0] = (y >= 0)? Y:-y;    t->used = (Y! = 0)? 1:0;    T->sign = (y >= 0)? 1:-1;    T->DP = p;    T->alloc = 1;    Bn_check (Bn_add_bn (z, x, t)); Clean:    return ret;}

              2.  single digit subtraction:

int Bn_sub_int (bignum *z, const bignum *x, const bn_sint y) {    int ret;    Bignum t[1];    Bn_digit p[1];    P[0] = (y >= 0)? Y:-y;    t->used = (Y! = 0)? 1:0;    T->sign = (y >= 0)? 1:-1;    T->DP = p;    T->alloc = 1;    Bn_check (Bn_sub_bn (z, x, t)); Clean:    return ret;}

        ★ Summary

to this position, the addition and subtraction algorithm of large integer is finished, the key to achieve addition and subtraction is the idea of classification, so that the complex problems can be simplified, and then conquer. The next few will focus on multiplication calculation, multiplication is more complex than addition and subtraction, and in the calculation, multiplication is time-consuming, so to do a lot of optimization work, otherwise the power multiplication will be very time-consuming.

"Back to this series of catalogs"

Copyright notice
Original blog post, reproduced must contain this statement, to keep this article complete, and in the form of hyperlinks annotated author Starrybird and this article original address: http://www.cnblogs.com/starrybird/p/4401863.html

Large integer algorithm [08] signed addition and subtraction

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.