You are here:

C++/enlarge the integer space

Advertisement


Question
hi can i enlrage the specified area for integer in memory ?
or how can i compute a vey big integer like this for example :87987987987654654654654654654654654654256311323198987
is there any way unlimited space for integer in memory mor than 4 byte or even 8 or 16 byte


Answer
There is no way to use unlimited space for an integer representation within a digital computer. The primary reason is that memory is finite and therefore not unlimited.

You can of course write code to handle big integer (and other numeric) values within the bounds of available memory if you understand the underlying concepts by using multiple existing built in integers (e.g. a 128 bit integer out of 2 64 bit integers or 4 32-bit integers). The problem comes in writing the logic to handle integer operations across multiple 'chunks' - division in particular is a challenge!

The details of big number - or arbitrary precision arithmetic - are not specifically a C++ problem. Unfortunately I do not have the time to write up all the ins and outs of such operations - even if I was totally familiar with them all (I know how to do addition and subtraction and can see the obvious ways to do the likes of multiplication and division but not the clever, more efficient, methods). This is especially so as this material is already available and it seems a needless waste of time me re-writing it (probably not as well <g>).

However, there are libraries available that do this sort of thing for you so I would suggest you start with one (or more) of those and then look deeper into how the magic works if you are interested - meaning both how C++ (or C) is used in particular implementations and the underlying algorithms used for the operations.

You might like to start at the Wikipedia article on arbitrary precision arithmetic at:

   http://wikipedia.org/wiki/Arbitrary-precision_arithmetic

You will notice it contains a table of available software packages quite a few of which are C or C++ libraries. I also found the Big Integer Library by Matt McCutchen at:

   http://mattmccutchen.net/bigint

Note that I have not tried any of these packages so cannot vouch for their usefulness. If you are interested in the hows and wherefores of doing arbitrary precision arithmetic then I suggest you read the article and follow some of the links to the other articles referenced - eventually you will get to ways to do some of the operations for example.

C++ libraries or bindings may present a more natural interface to big numbers than C libraries as they will hopefully take advantage of C++ only features - operator overloading and IOStream operators for example - so such big integer types look and behave much like built in integer types.

In the future, for other such questions, you might like to start by trying a few queries on an Internet search site such as Google (http://www.google.com) or Yahoo (http://www.yahoo.com) and at online encyclopedias such as Wikipedia (http://www.wikipedia.com) - this is all I have done here to locate the information presented as I have, at the moment, little need for such features myself.

If there is something you do not understand in the referenced material (e.g. some C++ or C idiom in a big number library you cannot get your head round) then please do ask further questions.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.