How to implement own biginteger class in java?

Posted on 2011-04-19
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
    LVL 47

    Accepted Solution


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

    Expert Comment

    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

    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

    Help provided, which should be acknowledged

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Understanding @ResponseBody annotation 5 36
    nested interface 5 29
    wordsWithoutList  challenge 24 61
    userCompare  challenge 3 54
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now