How to Create Your Own BigInt Class in Java — Data Structures and Algorithms Practice
By: Steve Pesce
Tags: Bigint, Large Numbers, Java, Data Structures, Algorithms

There are a couple ways to create a BigInt class — the most common being using a String or Integer Array. I will be using a String.

Setup

Here I am storing the value as a String and created some constructors, getters and setters. I give the option to set the value or create the object with an int or String.

I need a way to compare the values, so I am implementing the comparable interface and creating a compareTo() method.

Now we can start working on operations. Each of the following methods work just as they would when operating on numbers by hand.

Addition

For addition, we need to add the numbers digit by digit, right to left, while carrying over for any sum larger than 10. I push each sum to a StringBuilder and then reverse() it. The overflow (or the number being carried over) can only ever be 0 or 1, so I append a “1” to the number if it has an overflow on the last digit added. I also created a static helper method equalLengths() that gives me an array of both the operands as equal length strings. If one string is shorter than another, zeroes are added to the front of the string. I do this to make the character arrays parallel so that every digit lines up with the other in my for loop.

Note that I am not returning the result, but setting the current instance’s value to this result. If you want the result, you need to use the getter.

Multiplication

Multiplication is similar. We need to multiply each digit of the multiplier, starting from the right, by every digit of the multiplicand. On paper, every time I move over a digit, I write the product one step left. So for the method implementation, we need to keep track of the current power of 10, because every time we move one digit left, that product should be one power of 10 higher. Here is the methods flow:

the times method breaks the multiplier down to a char array

the last multiplier digit is sent to multiplyByInt()

Overflow is calculated each time by dividing by 10

The current product is pushed onto the StringBuilder

the digit of the multiplicand being multiplied is dropped

eventually an empty string ends up being sent to the function — then the exit condition will reverse the string and append zeroes(the total of this being the product of the current multiplier digit times the entire multiplicand)

it comes back to the times method, where it uses plus() to add the product to ‘this’.

this repeats for every digit of the multiplier, and the power of 10 increases on when each digit to the left is sent. This way, every time it comes back to the times() method, it can be added directly to ‘this’ (the same way that we shift each product left one digit when summing).

If I wanted a method that just multiplied a BigInt by an int, it would be a lot simpler, because we could just multiply the entire multiplier by each multiplicand digit. In fact, multiplyByInt() will work just fine if you give it any valid integer. You can do the same by hand in this manner.

Subtraction

Subtraction is done almost the same as addition. The difference here is that the “overflow” is taken away from numbers if they aren’t large enough to be subtracted from. So I created an overflow variable that is either 0 or -1.

If a number takes from another, it is set to -1, and then the next number has this digit taken away from it. We know if this needs to happen if ‘thisInt’ is less than ‘thisInt’. This method can result in some trailing zeroes, so I use a while loop to get rid of them.

Division

Division is tricky. To keep the blog post simple, I only allowed division by in integer. Division goes left to right. The overflow works just as we would do it on paper.

Factorial

Now we can implement a factorial method using our multiplication method. This is doing the same thing as the original solution, but using our newly made class instead of integers. Note that this is a static method. Give it an integer, and it will return a BigInt with a value of factorial(n).

Static Methods

Instead of having to deal with the objects directly when operating on large numbers, we can create some static methods. This way we can just send in the number strings and get strings back.

I also added a calculate() method. We can send it strings of the form ‘operand operator operator’ and it will return the resulting string.

Testing

Now we can test everything out, using the calculate method:

Which gives the correct output:

And we can also use it to calculate some giant numbers: