Solved

Long Integers

Posted on 2000-06-08
4
215 Views
Last Modified: 2010-04-02
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
Comment
Question by:new_user
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 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

by:nietod
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

by:nietod
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

by:new_user
ID: 2911784
Answer accepted
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now