Solved

# Long Integers

Posted on 2000-06-08
Medium Priority
261 Views
Hi all

How can I create a long integer number.
The integer must have a length from 0 to
300'000'000'000'000'000'000.

It must be in C++ on the WINDOWS Platform

Thanks

0
Question by:new_user
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3

LVL 22

Accepted Solution

nietod earned 200 total points
ID: 2911470
there are several approaches to this.  You can use a class/structure that stores several ordinary integers to represent a huge integer that has sufficient bits to represent the number.  You will then have to provide arithmetic functions for the class that perform the appropriate calculations on the numbers.   Another approach is to use BCD.  In this case you woudl have a class that stores a string that contains the digits represented.  Again you would have to write the mathmatical operations or the class.  BCD is slower in computation and takes up more memory, however it is easier to program and to use (you can check its value while debugging much more easily)  it is also much faster to convert to and from ASCII, so it is faster to enter and display.

continues
0

LVL 22

Expert Comment

ID: 2911535
For the first method, you will need to store 3 32-bit integers to have appropriate precission, like

class LongInt
{
int Low;
int  Mid;
int Hgh;
public:
class LongInt(int i = 0) : Low(i), Mid(0), Hgh(0) {};
class LongInt(const LongInt &Src) : Low(Src.Low), Mid(Src.Mid), Hgh(Src.Hgh) {};

LongInt &operator += (const LongInt &Rht)
{
// Note I beleive this works as the right way to set the carry
// but its been years.   You might need to do a little testing.
int Carry;
int Old

Old = Low;
Low += Rht.Low;
Carry = Low < Old:1:0;

Old = Low;
Mid += Rht.Mid += Carry;
Carry = Low < Old:1:0;

Hgh += Rht.Hgh += Carry;
};
};

LongInt operator += (const LongInt &Lft,const LongInt &Rht)
{
LongInt Result = Lft;
Result += Rht;
return Result;
}
0

LVL 22

Expert Comment

ID: 2911551
Hopefully that gives you enough to get started.  If you need more help I can provide it.  First you need to figure out which method you wnt to use.  Personally, I prefer the BCD route (I can probably help you more that way too) mostly because I can then use the data type more easily.  I.e. while debugging I can see what value is stored.  There is no easy way to check the value stored in the binary form, since the debugger shows 3 ints, not 1 int, and you can't even plug them into your calculator to find the right value, because it will exceed the precission of the calculator.
0

Author Comment

ID: 2911784
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month10 days, 21 hours left to enroll