• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compare two very large binaries

 
Ranch Hand
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have two arrays of booleans, just compare them, element to element.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic