SHARE
Facebook X Pinterest WhatsApp

MFC Template Class CLongInt

The template class template class CLongInt supports long integer arithmetic. Internally, a CLongInt is represented by n digits of the integral type T (specified by the second template parameter). T must be unsigned. The first digit is the most significant digit, the last digit is […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Mar 28, 2002
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

The template class template <UINT bits, class T = USHORT, class LT = ULONG> class CLongInt<bits> supports long integer arithmetic. Internally, a CLongInt is represented by n digits of the integral type T (specified by the second template parameter). T must be unsigned. The first digit is the most significant digit, the last digit is the least significant digit. The number base of the internal representation of a CLongInt is 2sizeof(T)*CHAR_BIT. For example, if T is an unsigned short, the number base of the CLongInt is 65536. For internal calculations, there must also exist an unsigned integral type LT (specified by the third template parameter) which must have double the size as T. To achieve maximum portability to other C++ compilers, I’ve chosen unsigned short for T and unsigned long for LT.

The template parameter bits specifies the size of a CLongInt in bits. bits should be at least 64+sizeof(T)*CHAR_BIT. For smaller bit numbers, use any of the built-in simple types int, long, or __int64.

The CLongInt class implements the complete integer arithmetic. There are friend operator functions to mix CLongInt operands with int or long operands. You can easily expand the class CLongInt to mix it with __int64 operands, too.

To prevent the specification of an invalid template parameter for T or LT, the technique of compile-time assertions is applied (see header file StaticCheck.h). This technique is presented by Andrei Alexandrescu in his book Modern C++ Design (see Chapter 2.1).

CLongInt class members:













Constructors:
CLongInt() Constructs an uninitialized CLongInt.
CLongInt(const CLongInt& u) Constructs a CLongInt from another CLongInt u.
CLongInt(int u)
CLongInt(UINT u)
CLongInt(long u)
CLongInt(ULONG u)
Constructs a CLongInt from an integral number u.
CLongInt(LPCTSTR psz, UINT nBase=10) Constructs a CLongInt from the string psz which
represents an integral number with the base nBase.
Public member functions:
int Compare(const CLongInt& v) const Returns zero if the CLongInts are identical, <0 if this CLongInt is less than v, or >0 if this CLongInt is greater than v.
CLongInt Divide(CLongInt v, CLongInt* pRem = 0, bool bSigned = true) const Divides this CLongInt by v. The quotient will be returned. The remainder will be stored in *pRem if pRem is unequal zero. If bSigned is false, the division will be performed unsigned.
int ToInt() const
UINT ToUINT() const
long ToLong() const
ULONG ToULONG()
Converts this CLongInt to an integral quantity of type int, unsigned int, long, or unsigned long.
CString ToString(UINT nBase = 10) const Converts this CLongInt to a string representing the number with base nBase.
Cast operator functions:
LPCVOID
LPVOID
Gives access to the internal digit buffer (e.g. for copying directly from memory to a CLongInt or vice versa).
Assignment operator functions:
=, +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=
Unary operator functions:
+, -, ++ (pre- and postincrement), (pre- and postdecrement), !, ~
Binary operator functions:
+, -, *, /, %, <<, >>, &, |, ^, <, <=, ==, !=, >=, >

Recommended for you...

Video Game Careers Overview
CodeGuru Staff
Sep 18, 2022
Dealing with non-CLS Exceptions in .NET
Hannes DuPreez
Aug 5, 2022
Online Courses to Learn Video Game Development
Ronnie Payne
Jul 8, 2022
Best Online Courses to Learn C++
CodeGuru Staff
Jun 25, 2022
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.