larger than long


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: larger than long

  1. #1
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    10

    larger than long

    Ok what I have to do is:
    Convert a binary string to a number.

    I can only use this interface
    Code:
    public interface Number {
           public void timesTwo();
           public void plusOne();
    }
    A class that uses a class which implements this inteface is as follows:

    Code:
    public class BinToDec {
    
    	// Converts the first parameter, a String representing a binary number,
    	// into a decimal value using the second parameter, an object that
    	// impements the Number interface and is initially zero.
    	public static void printDecimal(String bin, Number num) {
    		for (int i = 0; i < bin.length(); i++) {
    			num.timesTwo();
    			if (bin.charAt(i) == '1') {
    				num.plusOne();
    			}
    		}
    
    		// Print the result
    		System.out.println(num);
    	}
    }
    A Class called BigNumber has already been defined:
    Code:
    public class BigNumber implements Number {
    
    	private long value;
    
    	public BigNumber() {
    		value = 0;
    	}
    
    	public void timesTwo() {
    		value *= 2;
    	}
    
    	public void plusOne() {
    		value += 1;
    	}
    
    	public String toString() {
    		return new String("BigNumber: " + value);
    	}
    }
    And THAT is the one I have to improve so it can take on binary strings LARGER than 64 bits.



    This is the class that runs everything:

    Code:
    public class BinToDecApp {
    
    	// Converts a binary number specified at the command line into
    	// a decimal value and prints it to the screen
    	public static void main(String[] args) {
    
    		Number num = new BigNumber();
    		BinToDec.printDecimal(args[0], num);
    
    	}
    }
    I prefer to live in my own shadow. At least then I can be compared to a false past I'm more familiar with

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    What is your question?

  3. #3
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    10
    Oh sorry.

    Question is...
    How should I go about writing a new class that implements the Number interface that will be able to handle numbers longer than 32 or 64 bits?

    It is possible. I just don't know how to do it
    I prefer to live in my own shadow. At least then I can be compared to a false past I'm more familiar with

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    One way would be to have group/array of integers and cascade the overflow from one to the next, sort of the way you do it with single digits. Given that a digit holds values from 0 to 9, summing 9 + 8 gives 7 + carry of 1 to the next higher digit position.

  5. #5
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    10
    aaah, I think I understand with the whole 8+9 example.
    athough, I'm still not entirely sure of how this carry would be dealt with for lager than 64bits.

    btw, the algorithm only has to go up to 10,000 bits.

    so...lets say we have a signed long variable. The value is 0x7FFFFFFF (largest positive number), and I pop all those ones into an array of size 10,000. Then to go higher I would just put more ones in the later elements? (thus making the array backwards of the actual number, eg 0x44445555, will be read as 0x55554444)

    if you know what I mean....in otherwords starting the array at the least significant bit in the binary number...

    umm, that's all fine and easy, I think, just how will the array convert to a single number?
    I prefer to live in my own shadow. At least then I can be compared to a false past I'm more familiar with

  6. #6
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by Chirone
    umm, that's all fine and easy, I think, just how will the array convert to a single number?
    Take a look at the BigInteger class: http://java.sun.com/j2se/1.4.2/docs/...igInteger.html (it isn't really a number ofcourse)

  7. #7
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    10
    **** it, I must have left out another piece of information...

    I'm not allowed to use predefined java classes...

    primitives, arrays of primitives, and Strings only.

    but this BigInteger...if I knew how it worked...

    is it possible to get hold of the source code for the predefined classes in the java API?

    thanks for the tip though prometheuzz
    Last edited by Chirone; 09-20-2005 at 07:34 AM.
    I prefer to live in my own shadow. At least then I can be compared to a false past I'm more familiar with

  8. #8
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    The base 10 system we use for counting means that every position can hold a max of 10 values. For your large number system, change the base to what will fit in some larger holder. For example a byte can hold 256 values (unsigned). You could use bytes to define a base 256 number system to hold your large numbers. How many bytes in 10000 bits?
    Of you could use short to define a base 2 to the 16 -1 base system.
    The rules for addition are the same as with base 10 - starting from the right add two digits, if their sum exceeds the max value, carry 1 to the next higher level. For example with bytes, if you add two bytes and the sum is greater than 255, carry one to the next higher position.

  9. #9
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by Chirone
    **** it, I must have left out another piece of information...

    I'm not allowed to use predefined java classes...

    primitives, arrays of primitives, and Strings only.

    but this BigInteger...if I knew how it worked...

    is it possible to get hold of the source code for the predefined classes in the java API?

    thanks for the tip though prometheuzz
    No problem. The source of the BigInteger class should be in a zip file called src.zip in your JDK directory.

  10. #10
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    But here's an example you might give you some ideas. It's far from perfect and you can only add BigNumber, but it'll point you in the right direction I think:

    Code:
    public class BigNumber {
    
        int[] number = null;
    
        // ...
        public BigNumber(String num) {
            number = new int[num.length()];
            this.fillArray(num);
        }
        
        // ...
        private void fillArray(String num) {
            for(int i = 0; i < number.length; i++) {
                number[i] = Integer.parseInt(num.substring(number.length-i-1,number.length-i));
            }
        }
        
        // ...
        public void add(BigNumber bn) {
            int[] other = bn.getNumber();                     // get the other number-array
            int max = Math.max(number.length, other.length);  // get the max of the two numbers
            int[] sum = new int[max+1];  // create a new array wich is 1 bigger then the max
            int temp = 0, carry = 0;
    
            // loop through the new array
            for(int i = 0; i < sum.length; i++) {
    
                // both this.number and other have elements in them
                if(i < number.length && i < other.length) {
                    temp = (number[i] + other[i] + carry);
                }
                // only this.number has more elements
                else if(i < number.length) {
                    temp = (number[i] + carry);
                }
                // only other has more elements
                else if(i < other.length) {
                    temp = (other[i] + carry);
                }
                // we're at the end of the sum-array
                else {
                    temp = carry;
                }
    
                // if temp is 10 or larger
                if(temp > 9) {
                    carry = 1;           // carry 1 to the next round
                    temp -= 10;          // and subtract 10 from temp
                }
                else {
                    carry = 0;           // else, set carry back to 0
                }
    
                sum[i] = temp;           // set the new value
            }
            number = sum;                // set the new number
        }
    
        // ...
        public int[] getNumber() {
            return number;
        }
    
        // ...
        public String toString() {
            StringBuffer strb = new StringBuffer();
            for(int i = number.length-1; i >= 0; i--) {
                strb.append(number[i]);
            }
            return strb.toString();
        }
    }
    And a class to test it:
    Code:
    public class TestBigNumber {
        public static void main(String[] args) {
            BigNumber bn1 = new BigNumber("99999999999999999999999999999999999999999999999999999999999");
            BigNumber bn2 = new BigNumber("11111111111111111111111111111111111111111111111111111111111");
            System.out.println("Big number 1: "+bn1);
            System.out.println("Big number 2: "+bn2);
            bn1.add(bn2);
            System.out.println("\nNew big number 1: "+bn1);
        }
    }
    Good luck.

  11. #11
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    10
    thanks for the help prometheuzz
    Norm's comments had sparked off the idea that I have now. Which works, and is very simple.
    Altough I'm still to actually make the toString() method work properly, it does get me the right numbers.
    I prefer to live in my own shadow. At least then I can be compared to a false past I'm more familiar with

Similar Threads

  1. AnimateWindow API
    By Tim Manos in forum VB Classic
    Replies: 4
    Last Post: 10-19-2001, 07:06 AM
  2. Replies: 2
    Last Post: 10-18-2000, 11:50 PM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 01:11 PM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 10:16 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center