/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#ifndef INT_H
#define INT_H

#include <iostream>

using namespace std;

#define BITS_PR_CHAR 8
typedef unsigned char UCHAR;

//**********************************************************************
//
//  Class definitions:
//
//    BigInt   : general big integer
//    BigInt16 : 16 byte integer (=128 bit)
//    BigInt32 : 32 byte integer (=256 bit)
//
//**********************************************************************

// class for general big integer
class BigInt {
   private:
      // data
      int Size;
      UCHAR *Data;
      // dump
      void Dump(ostream& s) const;
   public:
      // constructor
      BigInt(int siz,int val = 0);
      BigInt(int siz,double val);
      BigInt(int siz,char *val);
      BigInt(const class BigInt& z);
      void operator=(const class BigInt& z);
      void operator=(const int z);
      // destructor
      ~BigInt();
      // get size
      inline int GetSize() const { return Size; };
      // high byte
      inline UCHAR HighByte() const { return Data[Size-1]; };
      // high long
      inline ULONG HighLong() const { return (ULONG(Data[Size-1])<<(3*BITS_PR_CHAR)) +
                                             (ULONG(Data[Size-2])<<(2*BITS_PR_CHAR)) +
                                             (ULONG(Data[Size-3])<<BITS_PR_CHAR) +
                                             ULONG(Data[Size-4]); };
      // leading equal bits (0's or 1's)
      int LeadingEqualBits() const;
      // operators
      friend class BigInt operator+(const class BigInt& z1,const class BigInt& z2);
      friend class BigInt operator-(const class BigInt& z1,const class BigInt& z2);
      friend class BigInt operator*(const class BigInt& z1,const class BigInt& z2);
      friend class BigInt div_help(const class BigInt& z1,const class BigInt& z2);
      friend class BigInt operator/(const class BigInt& z1,const class BigInt& z2);
      friend class BigInt operator%(const class BigInt& z1,const class BigInt& z2);
      friend BOOL operator==(const class BigInt& z1,const class BigInt& z2);
      friend BOOL operator!=(const class BigInt& z1,const class BigInt& z2);
      friend BOOL operator>(const class BigInt& z1,const class BigInt& z2);
      friend BOOL operator<(const class BigInt& z1,const class BigInt& z2);
      friend BOOL operator>=(const class BigInt& z1,const class BigInt& z2);
      friend BOOL operator<=(const class BigInt& z1,const class BigInt& z2);
      friend class BigInt operator+(const int z1,const class BigInt& z2);
      friend class BigInt operator-(const int z1,const class BigInt& z2);
      friend class BigInt operator*(const int z1,const class BigInt& z2);
      friend class BigInt operator/(const int z1,const class BigInt& z2);
      friend class BigInt operator%(const int z1,const class BigInt& z2);
      friend BOOL operator==(const int z1,const class BigInt& z2);
      friend BOOL operator!=(const int z1,const class BigInt& z2);
      friend BOOL operator>(const int z1,const class BigInt& z2);
      friend BOOL operator<(const int z1,const class BigInt& z2);
      friend BOOL operator>=(const int z1,const class BigInt& z2);
      friend BOOL operator<=(const int z1,const class BigInt& z2);
      friend class BigInt operator+(const class BigInt& z1,const int z2);
      friend class BigInt operator-(const class BigInt& z1,const int z2);
      friend class BigInt operator*(const class BigInt& z1,const int z2);
      friend class BigInt operator/(const class BigInt& z1,const int z2);
      friend class BigInt operator%(const class BigInt& z1,const int z2);
      friend BOOL operator==(const class BigInt& z1,const int z2);
      friend BOOL operator!=(const class BigInt& z1,const int z2);
      friend BOOL operator>(const class BigInt& z1,const int z2);
      friend BOOL operator<(const class BigInt& z1,const int z2);
      friend BOOL operator>=(const class BigInt& z1,const int z2);
      friend BOOL operator<=(const class BigInt& z1,const int z2);
      friend class BigInt operator>>(const class BigInt& z,int b);
      friend class BigInt operator<<(const class BigInt& z,int b);
      friend istream& operator>>(istream& s,class BigInt& z);
      friend ostream& operator<<(ostream& s,const class BigInt& z);
      // functions
      friend class BigInt pow(const class BigInt& z,int p);
      // special functions
      friend class BigInt DoubleSize(const class BigInt& z);
      friend class BigInt HalfSize(const class BigInt& z);
      friend int intExtract(const class BigInt& z);
   };

// class for 16 byte integer (=128 bit)
class BigInt16 : public BigInt {
   public:
      // constructor
      BigInt16(int val = 0);
      BigInt16(double val);
      void operator=(const class BigInt& z);
      // destructor
      ~BigInt16();
   };

// class for 32 byte integer (=256 bit)
class BigInt32 : public BigInt {
   public:
      // constructor
      BigInt32(int val = 0);
      BigInt32(double val);
      void operator=(const class BigInt& z);
      // destructor
      ~BigInt32();
   };

#endif // INT_H
