wood burning stoves 2.0*
The moose likes Programming Diversions and the fly likes compare two very large binaries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "compare two very large binaries" Watch "compare two very large binaries" New topic
Author

compare two very large binaries

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hi all ,
suppose I have two arbitrary lenght binary numbers (could be very large binaries).
exple : 1000111 & 11111110001

is it possible to compare them using only there 0s & 1s representations.
i.e I don't want to convert them to base 10 & compare them (probably using BigInteger).
what I'm looking for is an algorithm that compares 1000111 & 11111110001 only using there 0s & 1s sequences.
If this is possible how can I implement it ?
please help (this is urgent).
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
java.util.BitSet?


Please change your display name to conform with our JavaRanch Naming Policy. You can change it via the My Profile link. Otherwise we will suspend your account.
Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Sorry about display name ...I have rectified the problem.

BitSet is of no help : there is no compareTo method or am I wrong ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There is a method equals() which returns true if the two BitSets have exactly the same bits set. Check the API.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Im' using an array of boolean[] to hold my binaries.
I have Read somewhere that boolean types are internally optimized in java.but I don't know why & how..
can you shed some light on this issue ?

I also have another question regarding non-negative binaries operations :
How the shift right work for non-negative binaries ?
for exple :
1001 shiftRight(6) = ?
1001 shiftRight(2) = ?

thanks.
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
If you have two arrays of booleans, just compare them, element to element.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
How are you getting your binaries? If you have strings, you can compare length (after trimming leading '0's). The longer is larger. If they are the same length, compare from left to right. The first time one is '1' and the other is '0', the one with the '1' is larger. They are equal if you get to the last bit and they are also the same.

As to your second question, you simply need to add '0's to the left side of your representation, and drop the right bits. Once again, it will depend on how you are representing the bits.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hi Timmy ,
Yes Your algorithm is working fine thanks
this is the right solution of this problem & I did managed to implement this compare() method.

Well said timmy.. thanks again
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: compare two very large binaries
 
Similar Threads
Unicode and char encoding ??
How to have characters which the phone doesn't support
binaries and byte codes
Binary Numbers Comparison.
binary file to text file