• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare two very large binaries

 
Yahya Elyasse
Ranch Hand
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yahya Elyasse
Ranch Hand
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have two arrays of booleans, just compare them, element to element.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic