Arbitrarily Large Numbers

Encryption algorithms often use very large numbers, well beyond the normal 32 or 64 bit limit imposed by the long integer type. When faced with this problem an attractive solution was to create a large number class which could be treated in the same way as a normal integer but did not have an upper size limit. In fact this turned out to be far more time consuming than writing the encryption algorithm itself. Here are the files needed to use the class:

Please run the test program before using the large number class with your problems. For the correct operation of the test the constant BIT_INCREMENT within LargeNumber.C should be set to 4 and reset to 32 afterwards. In the test program all operators are used and the results displayed. On each line will be a normal integer which should be followed by the positional code representation of the same integer (0 = 0, 1 = 1, 2 = 10, 3 = 11, 4 = 100, -4 = -100). After the main operator tests is an input test phase and you will be asked to enter some numbers. Please type in the positional code representation of the requested number and press return, this same value should then be displayed as the result. If there are mismatches for any of the lines then something is wrong then the class is not functioning correctly.

The large number class works by storing the positional code representation of its integer value in a char array. Each array index contains either '/0' or '/1' for binary 0 and 1 respectively. If the integer ever grows enough to overflow the array then new space is automatically allocated, the old value copied over, and the old space freed up. A boolean flag indicates whether the integer is negative or not. All standard integer operators are present in the class along with some others such as odd and even checks which can be implemented more efficiently using the internal information of the class.

Although this class can be used in the same way as ordinary integers here are a few hints on how to get the best performance:

  1. Avoid operators such as +, -, and * and instead try to use the equivalent +=, -=, or *= operator even if this involves breaking a calculation onto several lines. This avoids temporary variables which must be initialised and take up space on the heap.
  2. Declare variables in the outermost block possible, avoid declaring in loops. For similar reasons to above this wastes less time.
  3. Try to reuse variables as this reduces initialisation and may also cut down on the number of times overflows must be dealt with.
  4. Always use pre-increment in preference to post-increment, the latter requires temporary a variable.