
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


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

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.

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

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)

**** 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; 09202005 at 06: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

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.

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.

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.lengthi1,number.lengthi));
}
}
// ...
public void add(BigNumber bn) {
int[] other = bn.getNumber(); // get the other numberarray
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 sumarray
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.length1; 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.

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

By Tim Manos in forum VB Classic
Replies: 4
Last Post: 10192001, 06:06 AM

By Ani in forum VB Classic
Replies: 2
Last Post: 10182000, 10:50 PM

By Julian Milano in forum VB Classic
Replies: 2
Last Post: 08112000, 12:11 PM

By Julian Milano in forum VB Classic
Replies: 0
Last Post: 08102000, 09:16 PM

By Kunal Sharma in forum VB Classic
Replies: 2
Last Post: 04252000, 03: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

Forum Rules

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