2

i have 2 very large binary numbers (144 digits). I want to write them in different RandomAccessFiles and then read the files to memory and check to see which number is bigger. What i did so far:

1. I created a BigInteger:

BigInteger big = new BigInteger("01110101010010101010111100010101010101010101010110101010101010101010010101010101010101010101010101111010010101010",2);

2. I get the longValue:

big.longValue();   

3.. I write the long to a randomaccessfile,read the files,compare the longs etc...

But if the binary is longer than the 'Long.maxvalue' what i did is wrong, correct?

So does anyone have any suggestions?

Can i handle large binary numbers otherwise?

Sarathi Kamaraj
  • 647
  • 3
  • 9
  • 26
George
  • 21
  • 2
  • 2
    Why dont you just write the big integer to file? BigIntegers are serializable by default. Check on ObjectInputStream. You can also check on simply writing the string equivalent to file. – maress Oct 15 '13 at 09:57
  • You could use a [bit stream](http://www.cs.duke.edu/courses/cps100e/spring11/assign/huff/code/BitInputStream.html). Theoretically a long should not be used to represent bits. – Mr. Polywhirl Oct 15 '13 at 09:58

3 Answers3

1

try comparing as below

           BigInteger big1 = new BigInteger("01110101010010101010111100010101010101010101010110101010101010101010010101010101010101010101010101111010010101010",2);
           BigInteger big2 = new BigInteger("01110101010010101010111100010101010101010101010110101010101010101010010101010101010101010101010101111010010101010",2);
           int result =big1.compareTo(big2);
           System.out.println(result);
upog
  • 4,965
  • 8
  • 42
  • 81
  • Since i am using RandomAccessFile i would like to be able to seek. I don't know much about serialization, can i write my BigInteger serialized as String?I want to know how many bytes it will need on the file so as to seek when i read the files. That's why i convert it to long (8 bytes).I want to a file: write int,write int,write large binary,write int,write int,write large binary....etc and then be able to read all the values and compare the binary numbers. – George Oct 15 '13 at 10:12
0
  1. eliminate initial zeros

  2. Convert these numbers to Strings

  3. if(String1.length !=String.length ) maximum number is the longer one

else

take one by one digit from the beginning and compare.

if(string1 digit x = 0 & string 2 digit x=0) go to next digit

if(string1 digit x = 1 & string 2 digit x=0) String 1 is big

if(string1 digit x = 0 & string 2 digit x=1) String 2 is big

Madhujith
  • 157
  • 4
  • 18
0

Obviously, your number is too large for a long.

I recommend you to export your BigInteger as a byte array using BigInteger.getByteArray(), and then you can save it to a file (that's what we do in cryptography).

Also, the byte array can be converted back to a BigInteger using its contructor.

Vincent Cantin
  • 16,192
  • 2
  • 35
  • 57
  • So vincent and upong thanks a lot. What i should do is convert the binary number to BigInteger, write it to file using getByteArray(), read it from file using constructor BigInteger(byteArray[]) and then compare the BigIntegers using big1.compareTo(big2). I sum up for anyone else having same problem. Thanks again! – George Oct 15 '13 at 10:35
  • You're welcome. Remember to mark this answer as the solution if you see fit. – Vincent Cantin Oct 16 '13 at 02:43