The conversion of the basic arithmetic of the super-long integer to the input system

Source: Internet
Author: User

Decimal Turn binary

Since a single "bit" takes 216-1 as the theoretical maximum, each similar "decimal" bit can be represented by a 16-bit binary in the representation of this large integer, and the symbol bits are represented separately.

The conversion of "ten" into binary is actually transformed by the intermediate state (that is, the logical storage representation of a large integer). In the conversion process, each "bit" of the large integer is not coupled, there is no dependency, so the implementation of a relatively simple, that is, the decimal number is continuously in addition to 2 of the remainder of the way the results of the binary. It is important to note that a binary string of less than 16 bits needs to be padded with "0" in the high position to 16 bits.

Because of the relationship that is represented by traversing large integers (starting from the low) (the high position of an integer is the high position of the array subscript), the binary string that results in is reversed in its entirety. For general purposes, if the left start character of the string is flipped, the 0 is filtered because these 0 have no practical meaning.

The decimal is converted to binary char* dectobinhbint (hbigint* src) {char *res;un_short cur_num;long index;int j;//sign bit + string Terminator ' +2res ', so the = (c Har *) malloc (sizeof (char) * (src->length*bit_pre_word+2)); memset (res, ' n ', sizeof (char) * (src->length*bit_pre _word+2)); for (index=0; index < src->length; index++) {cur_num = Src->pbigint[index];for (j=0; j<bit_pre_ word;j++) {res[index*bit_pre_word+j]= (char) ((Cur_num & 0x1) +48);//+48 is to do ASC code conversion cur_num >>= 1;} if (src->sign = =-1) {res[index*bit_pre_word]= '-'; res[index*bit_pre_word+1]= ';} else res[index*bit_pre_word]= ' REVERSTR (RES); Reverse string Trimleft (res); The left meaningless "0" character, until the character "1" appears return res;
Binary Conversion Decimal

The implementation principle first divides the string from right to left into 16 fixed character length segments, if the last less than 16 bits do not need to be filled, because when traversing the binary string, when the length of the string will not continue to traverse, so there is no overflow situation. Set a repeating counter to the 16-bit segment and accumulate, and use large integers to set the current expression bit as cumulative sum, and reset the counter (0). The last set of 16 strings (probably less than 16 bits) is traversed to the last character (definitely 1) depending on the actual length.

The binary is converted to decimal int bintodechbint (char* bin, hbigint* src) {char *res;un_short cur_num=0;long index,str_len;int i=0,j=0; Trimleft (BIN), res = Bin;str_len = strlen (Bin), Extendhbint (Src,str_len/bit_pre_word + 1); for (index=str_len-1; index >= 0; index--) {Cur_num + = ((res[index]-48) << j); if (0 = = (j+1)%bit_pre_word) {src->pbigint[i++] = Cur_num;j=0;cur_nu M=0;continue;} j + +;} if (Str_len%bit_pre_word) src->pbigint[i++] = Cur_num;src->length = I;return return_ok_bint;}

Summary:

For the basic operation of the super-long integers are all introduced, the following will be used in the private functions (input, output, shift, assignment, exchange, etc.) posted, please pay attention!





The conversion of the basic arithmetic of the super-long integer to the input system

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.