[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to implement own biginteger class in java?

Posted on 2011-04-19
Medium Priority
Last Modified: 2012-05-11
Hello experts,

Please help me to implement my own BigInteger class in java. I'm totally lost in this assignment.
I should hold the biginteger value in some array (array of bytes) in little endian, but i don't now how to do this.
If it's too difficult, implement/override the methods toString(), equals(Object obj), compareTo(BigInteger value) can be helpfull.

Thank for help

here is my demo:

public class BigInteger implements Comparable<Object> {
	private boolean signum; 
	public BigInteger(){}
	public BigInteger(long value){}
	public BigInteger(BigInteger value){}
	public BigInteger(String value){}
	public String toString(){
	public boolean equals(Object obj){
	public int compareTo(BigInteger value) {
	public int compareTo(Object o) {
	public double doubleValue(){
	public double floatValue(){

	public double intValue(){
	public double longValue(){
	public int getMagnitudeLength(){
	public BigInteger add(BigInteger value){
	public BigInteger add(int value){

public static void main (String[] args){
		BigInteger c1 = new BigInteger(1234);
		System.out.println("c1 = " + c1);

		BigInteger c2 = new BigInteger(c1);
		System.out.println("c2 = " + c2);

		BigInteger c3 = new BigInteger("-456");
		System.out.println("c3 = " + c3);
		System.out.println("c1 == c2: " + c1.equals(c2));
		System.out.println("c1 == c3: " + c1.equals(c3));
		System.out.println("c1 ? c3: " + c1.compareTo(c3));
		c2 = c2.add(100).add(c1);
		System.out.println("c2 = " + c2);

Open in new window

Question by:drahos
  • 2
  • 2
LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 35428105

You can use the real implementation as a reference, how to do some pieces of it:
LVL 47

Expert Comment

ID: 35428131
pay attention to this comment from the source above having to do your little-endian requirement:

 /** All integers are stored in 2's-complement form.
  63:    * If words == null, the ival is the value of this BigInteger.
  64:    * Otherwise, the first ival elements of words make the value
  65:    * of this BigInteger, stored in little-endian order, 2's-complement form. */
LVL 86

Expert Comment

ID: 35428139
BigInteger classes are usually String-oriented. You might like to store each digit in ascii in the array beginning with the most significant digit
LVL 86

Expert Comment

ID: 35467640
Help provided, which should be acknowledged

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question