Big integer algorithm [03] comparison operation, integer algorithm 03 operation

Source: Internet
Author: User

Big integer algorithm [03] comparison operation, integer algorithm 03 operation

In the previous blog, I briefly talked about some basic operations of the big integer algorithm. Next I will introduce some relatively advanced operations: Comparison operations. Comparison is divided into three types: absolute value comparison, symbol number comparison, multi-Precision comparison and single-Precision comparison.

Convention: for all comparison operations, if x> y, return 1; x = y, return 0; x <y, return-1.

 

★Absolute Value Comparison

The absolute value comparison operation only compares the absolute values of two large integers. The comparison method is the same as that of the decimal number. Compare the number of digits first, and the absolute value of the number of digits is larger. If the number of digits is the same, compare the values of the two big integers from the high to the low to the same, if the digit of an integer is greater than the digit at the same position in another integer, the integer is larger.

Int bn_cmp_abs (const bignum * x, const bignum * y) {size_t I; if (x-> used> y-> used) return 1; if (x-> used <y-> used) return-1; // The number of digits of x and y is the same, and the size of the same bit is compared in sequence. For (I = x-> used; I> 0; I --) {if (x-> dp [I-1]> y-> dp [I-1]) return 1; if (x-> dp [I-1] <y-> dp [I-1]) return-1;} return 0 ;}

 

★Comparison of signed numbers

The comparison of signed numbers is a little more complex than the absolute values, but it can be summarized into several categories:

1. x and y different numbers: non-negative numbers are greater than negative numbers.

2. the numbers x and y are the same: if both x and y are non-negative integers, the absolute values of x and y are compared. If both x and y are negative integers, the absolute values of y and y are compared.

The first case is easy to understand. I will explain the second case.

If x> = 0 and y> = 0, if x> = y (x <y ), | x |> = | y | (| x | <| y |). Therefore, the absolute values of x and y must be compared.

If x <0 and y <0, if x> = y (x <y),-x <=-y (-x>-y) exists ), that is, | x | <= | y | (| x |> | y |). Therefore, compare the absolute values of y and x (compare the number of unsigned values in the opposite direction ).

int bn_cmp_bn(const bignum *x, const bignum *y){    if(x->sign == 1 && y->sign == -1) return  1;    if(y->sign == 1 && x->sign == -1) return -1;    if(x->sign == 1)        return bn_cmp_abs(x, y);    else        return bn_cmp_abs(y, x);}

 

★Comparison between multi-precision and single-precision

Simply put, a large integer is compared with a signed Single-precision number (bn_sint type. With the signed comparison operation between the preceding big integer and the big integer, you do not need to write a new operation here, assign a signed Single-precision value to a temporary big integer variable t and call the bn_cmp_bn function for comparison. Although this deceptive trick may compromise a little bit of performance to some extent, it can make coding and computing relatively simple.

Int bn_cmp_int (const bignum * x, const bn_sint y) {bignum t [1]; bn_digit p [1]; p [0] = (y> = 0 )? Y:-y; t-> sign = (y> = 0 )? 1:-1; t-> used = (y = 0 )? 0: 1; // note that when bignum is 0, used = 0. t-> alloc = 1; t-> dp = p; return bn_cmp_bn (x, t );}

 

★Summary

Although the comparison operation is simple, I made several low-level errors at the beginning of implementation. In the final analysis, I still did not take all the situations into consideration, and the encoding was not careful enough. So I am still saying: details determine success or failure. The next article will introduce more advanced operations: Bit setting operations.

 

[Back to the directory of this series]

 

Copyright Notice
Original blog, reproduced must contain this statement, to maintain the integrity of this article, and in the form of hyperlink to indicate the author Starrybird and the original address of this article: http://www.cnblogs.com/starrybird/p/4354703.html

Related Article

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.