Technical support e-mail : mktmk<at>

Home pageааааааааааааааа :


Multiprecision unsigned number template library (MUNTL)

Distributed under the GNU LGPL.


Users guide.


1. The purpose of program.


The program is intended for the organization of calculations with the big

precision for unsigned numbers.


2. Conditions of application.


The program is written on "pure" C++ with template. The program does not

use the assembler. The program is tested by BCB60 in ANSI-compatibility mode.

Configuration of test computer: Athlon 1800, Chaintech 7AJA2, 256Mb RAM.

Test time (file dit.cpp) is 42 second.


3. The problem description.


Sometimes it is necessary to have unsigned numbers which length exceeds

ability of a current platform. For example:


а- Storage of the big numbers (for example factorials) and operations with them.

а- Emulation of registers of the big length.

а- Indication of many error in position code.


This program is intended for the decision of these problems.


4. The input and output data.


4.1 General description.


Numbers of the big precision are realized as template class di_unsigned.

This template class have the overloaded arithmetic operations. Due to this,

di_unsigned may use as usual unsigned numbers.


Word length of numbers di_unsigned do not limited.


Inside a class di_unsigned the information is stored as array.


4.2 Structure of the project.


Fileа di.hаааааааааа - is template library.

Files dit.cpp, dit.h - is the test of template library.

Fileа die.cppааааааа - is an example of use template library.

Fileа readrus.txtааа - is this users guide in Russian.

Fileа readeng.txtааа - is this users guide in English.


4.3 Parameters of template.


The template di_unsigned have 3 parameters.


template <class T,class Tunit,T size> class di_unsigned;


The first parameterа "T"аааа - is type of the counter of bits.

The second parameter "Tunit" - is type of internal array which stored the

аааааааааааааааааааааааааааааа information.

The third parameterа "size"а - is numerical parameter. Determines length

аааааааааааааааааааааааааааааа di_unsigned in bits.


Type "T" should may contain number "size". For example if "T" is unsigned char,

parameter "size" can not be more than 255.


Type "Tunit" should be unsigned for correct work of right shifts operator.


For increase of performance it is recommended to choose "Tunit" equal to

machine word for the current platform. For win32 it is recommended use

"unsigned int" as "Tunit".


Unsigned char and char of the current platform should be one size for correct

work functions "printtostring".


Unsigned char and char of the current platform should have even number of bits.

Problems are possible on the platforms having CHAR_BIT=7.


Is possible use of di_unsigned with different parameter "size" in one

arithmetic expression (see example in a file die.cpp).


This version not supported use of numbers with different types "T" or "Tunit"

in one arithmetic expression.


Type "T" should may contain size+size1 for correct work of operation of

multiplication. (Size and size1 is parameter "size" of two di_unsigned operand

in binary operator).


Type "T" should may contain size+1 and size1+1 for correct work of operation of



4.4 Example of use di_unsigned.


// Inclusion of library of templates di_unsigned

#include <di.h>

// Inclusion of library of standard input-output

#include <stdio.h>


// Word length of the first type

const unsigned int numberbit1=250;


// Word length of the second type

const unsigned int numberbit2=253;


// Definition of the first type

typedef di_unsigned < unsigned int, unsigned int, numberbit1 > big_digit1;


// Definition of the second type

typedef di_unsigned < unsigned int, unsigned int, numberbit2 > big_digit2;


// Use "unsigned int" as T and Tunitа


int main (int argc, char* argv []) {


// Length of a text string for print first number

const unsigned int l1 = ((numberbit1)/CHAR_BIT+1)*2+1;


// Length of a text string for print second number

const unsigned int l2 = ((numberbit2)/CHAR_BIT+1)*2+1;


// Text string for print first number

char string1 [l1];


// Text string for print second number

char string2 [l2];


// Initialization of three numbers

big_digit1 d1=1;

big_digit2 d2=1;

big_digit2 d3;


// left shift operatorа

d1 <<= (numberbit1/2+1);

d2 <<= numberbit1/2;


// increment operator



// print of result

d1.printtostring (string1, l2);

d2.printtostring (string2, l2);


printf (" \nd1аа =0x%s ", string1);

printf (" \nd2аа =0x%s ", string2);


// addition operator



// print of result

d3.printtostring (string2, l2);

printf ("\nd1+d2=0x%s", string2);


// multiplication operator



// print of result

d3.printtostring (string2, l2);

printf ("\nd1*d2=0x%s", string2);


// division operatorа



// print of result

d3.printtostring (string2, l2);

printf ("\nd1/d2=0x%s", string2);


// modulus operator



// print of result

d3.printtostring (string2, l2);

printf ("\nd1%sd2 =0x%s", "", string2);


return 0;



4.5 The description of functions - members.


For a template di_unsigned the following operators are determined:


&а - bitwise AND

&= - bitwise AND with assignment

|а - bitwise inclusive OR

|= - bitwise inclusive OR with assignment

^а - bitwise exclusive OR

^= - bitwise exclusive OR with assignment

~а - bitwise complement


=а - assignment


== - equalityааааааааааааааааааааа

!= - inequality

>а - greater than

<а - less than

>= - greater than or equal

<= - less than or equal


<<а - left shift

<<= - left shift with assignment

>>а - right shift

>>= - right shift with assignment


+а - addition

+= - addition with assignment

-а - subtraction

-= - subtraction with assignment

++ - preincrement and postincrement

-- - predecrement and postdecrement


*а - multiplication

*= - multiplication with assignment

/а - division

/= - division with assignment

%а - modulus operator

%= - modulus operator with assignment


Additional functions:


void clear(); - set "all zero"

void set();аа - set "all one"


void clear(const T bit); - clear a bit with specified number

void set(const T bit);аа - setаа a bit with specified number


bool tst(const T bit) const; - check a bit with specified number. Return

ааааааааааааааааааааааааааааааа true, if bit is 1 and false in opposite case.


const T getbite() const;аааа - returns precision of number (parameter "size" of

аааааааааааааааааааааааааааааа template)


bool getcf() const;ааааааааа - returns a status of carry. It is established

аааааааааааааааааааааааааааааа after operations of addition and subtraction.


di_version ver() const;ааааааааа - returns version of library

di_serialnumber sernum() const;а - returns serial number of library


operator const Tunit * () const; - cast to pointer of Tunit. (Return internal

аааааааааааааааааааааааааааааааааа array. Do not modify it.)


void printtostring (char* c_str, T lengthstr); - prints the number to c_str

аааааааааааааааааааааааааааааааааааааааа ааааааааin hex format .


di_unsigned(); - constructor without parameters. It do not clear internal array.


di_unsigned (const di_unsigned& d); - constructor


template <T size1>

di_unsigned (const di_unsigned <T,Tunit,size1>& d); - constructor


template < class Tv >

di_unsigned (const Tv& val); - constructor. "Tv" should be unsigned.

аааааааааааааааааааааааааааааа For example unsigned int, unsigned long int.


di_unsigned(const char* c_str); - constructor. The line c_str should contain

аааааааааа аааааааааааааааааааааааhex string with zero at end. Any invalid

ааааааааааааааааааааааааааааааааа symbol finishes convertion.


Operators of assignment. Parameters - as in the appropriate constructors.


di_unsigned& operator = (const di_unsigned& d);


template <T size1>

di_unsigned& operator = (const di_unsigned <T,Tunit,size1>& d);


template < class Tv >

di_unsigned& operator = (const Tv& val);


di_unsigned& operator = (const char* c_str);


4.6 Use of functions - members.


The binary addition operator "+" returns number with the greater length from

two operands. For example, at addition of numbers of 100 and 200 bits this

operator will return result with length of 200 bits.


The binary multiplication operator "*" returns number with the length equal

to the sum of lengths two operands. For example, at multiplication of numbers

of 100 and 200 bits this operator will return result with length of 300 bits.


The binary division operator "/" returns number with the length equal to

length divided.


The binary modulus operator "%" returns number with length equal length of



If user define DI_EXCEPTIONENABLE macro, the library will throw exception if

division by zero will occur.


class di_exception {





By default DI_EXCEPTIONENABLE do not define.


Any operators and functions have not check correct of their parameters.

For example, if to apply function set(200) to number with length of 100 bits,

will take crash of programs.


Сайт создан в системе uCoz