Unsigned large integer classes that can be used in number theory calculations

Source: Internet
Author: User
Tags comparison integer

A few days ago, inadvertently visited the science network, which introduced many number theory problems, which is my hobby, so I used to write a free time for the calculation of numbers of the unsigned large integer class.

The basic structure of a classClass CUSuperInt
{
public:
//构造及析构函数
CUSuperInt();
CUSuperInt(DWORD dwValue);
CUSuperInt(char* pszVal);
CUSuperInt(CUSuperInt& x); 
virtual ~CUSuperInt();
protected:
DWORD *pValue; //指向一个DWORD数组,用于存放数值
DWORD len;   //DWORD数组的长度
DWORD last;  //数组中的有效长度
};

The class name is Cusuperint, and the second letter represents the unsigned meaning. Of course, as long as you add a symbol to the member, and then simply expand into a signed large integer class.

Pvalue points to the DWORD array using dynamic allocation strategy, when the length of the array is not enough, the strategy is multiplied, that is, the length becomes 2*len. (This may not be the best distribution, but it simplifies the design)

The last member indicates the effective length of the data in the array, which reduces the amount of unnecessary operations. The last member minimum value is 1, when the last=1 is actually transformed to a DWORD.

Second, the constructor

class defines four constructors, and one of the constructors ' arguments is a string pointer that represents converting a string into a Cusuperint class. Such a string can be a decimal or hexadecimal string, expressed in the same way as the C language specification, such as "12345678901", "0x123456789abcdef", which represents a decimal number, and the latter represents a hexadecimal number. At the same time in order to facilitate the application, also allows the number of strings in the middle with an empty cells section, such as "12345 567890", "0x123 4567 89ab cdef" and so on.

Third, overloaded operators

Overloading the assignment operator, you can assign a DWORD, string, and Cusuperint to a Cusuperint object.

Overloaded with subtraction and other arithmetic, and ++,―― operators. Note, however, that for the/= operation the return value is the remainder and the quotient is in the left operand. For the/operator return, the remainder is lost.

The% operator is overloaded, and can be calculated as a modulo.

The comparison operator is overloaded, and a comparison operation can be performed to return a bool value.

Defines a dobule () member used to multiply the N-squared by 2, a useful logical left-shift algorithm.

Defines a half () member used to divide the n times by 2, using the logical right SHIFT algorithm, so this function will lose the remainder.

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.