質問

Possible Duplicate:
How to implement big int in C++

For example, say I have a variable of the "long" type. A long can store values from -⁠9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Is it possible for me to create my own C++ data type that allows me to create integers of unlimited size? I know it's kind of reckless from a memory standpoint, but I'm just curious.

役に立ちましたか?

解決

Ha! Here is a reckless answer!

Just create a class that dynamically creates and adds bit sets. To implement the class, you should create a vector (or any appropriate container you want/need) that store unsigned longs (each unsigned long represents a bit set of higher dimension than that of what precedes it; you can also use any POD types you want).

For the class to have virtually unlimited possible values, you should implement automatic detection (preferably on operators) for value overflows and underflows. For overflows you should then add a bit set of higher dimension after the last bit set. Underflows should be handled by setting a reference bit set that contains the value zero. If the value becomes lower than zero (or the lowest value), you should then add a bit set before the reference (or that which contains the lowest value) bit set.

Please read about binary arithmetic and base numbers. They should give you the idea (and are very useful indeed!).

他のヒント

Yes, just implement it (or google). One example is http://sourceforge.net/projects/cpp-bigint/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top