Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Class for Representing Large Integers


November 1996/A Class for Representing Large Integers/Listing 1

Listing 1: BigNum header file


// (c) 1996 tony breitzman
// BigNum.h - a class for operating on arbitrarily large integers

# include <fstream.h>
# include <stdlib.h>
# include <string.h>
const int BASE = 10000;

class BigNum{
 protected:
  int *x;               //this array holds the BigNum
  char positive;        //sign flag
  long exp;             //array index of most significant digit

 public:
  BigNum(char *s);                        //construct from string
  BigNum(long k=0,long new_sz=0);         //construct from long
  BigNum(const BigNum &b,long new_sz=0);  //copy constructor
  ~BigNum(){delete[] x;}                  //destructor

  friend ostream& operator<<(ostream & os, BigNum &b);  //for printing

  BigNum & operator=(const BigNum &b);  //assignment
  BigNum & operator=(long k);           //assignment from long
  BigNum operator+(const BigNum & b);   //addition

  BigNum operator-(const BigNum &b);    //subtraction
  BigNum operator/(const BigNum &den);  //divide bignums
  BigNum operator/(long denom);         //divide bignum by long
  BigNum operator*(const BigNum & b);   //bignum multiplication
  BigNum operator^(const long n);       //exponentiation

  //easy routines; these are easily implemented and are not
  //included here to save space. The complete listings may be
  //obtained from my web page.
  BigNum operator-();                   //unary minus
  BigNum operator+(const long k);       //addition with long
  BigNum operator-(const long k);       //Bignum - long
  BigNum operator*(long k);             //multiply bignum * long
  BigNum operator%(const BigNum &denom);//remainder of division
  long operator%(const long denom);     //rem. Of bignum / long
  char operator<=(const BigNum &b);
  char operator>(const BigNum &b);
  char operator>=(const BigNum &b);
  char operator==(const BigNum &b);
  char operator<(const BigNum &b);
  char operator<=(const long k);
  char operator>(const long k);
  char operator>=(const long k);
  char operator==(const long k);
  char operator<(const long k);
  char is_zero();               //returns 1 if value of BigNum is zero
  void asl(const long n);       //arithmetic shift left; equivalent to
                                / multiplying by base n times
  void asr(const long n);       //arithmetic shift right
  long BigNum2Long();           //convert a bignum to a long
                                //if possible
  BigNum abs(const BigNum &b);  //absolute value
  void random(const int size);  //makes a random bignum of size digits
};
// End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.