• 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

Binary Numbers Comparison.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all ,

I have two arbitrary length binary numbers.
ex : 100011111 & 1111001110001
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).

If its possible how can I implement it
Thanks.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have to convert to decimals. Every bit is 2^x so you at least need to find the bits that are 1 and calculate what x is for that bit. Without converting to decimal I think the faster way to get the most significant bit that is 1 of each number and compare that. Oh you do need to watch out for negatives (such as first bit is 1).
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Heimdall Ksu wrote:Hello all ,

I have two arbitrary length binary numbers.
ex : 100011111 & 1111001110001
Is it possible to compare them using only there 0s & 1s representations.



How are you storing these numbers? As Strings?

Heimdall Ksu wrote:i.e I don't want to convert them to base 10 & compare them...



In Java, all numbers are in base 10. Yes, you can use octal or hexadecimal literals, but their value will still be stored as a decimal (base 10).
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In Java, all numbers are in base 10. Yes, you can use octal or hexadecimal literals, but their value will still be stored as a decimal (base 10).



Didn't you mean that all numbers (ie: numeric primitives) are stored in binary and then by default formattted for output as decimal?
 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I am storing them as strings.
I padded them to equal length and then used the compare function in String class and got the result I was looking for.

Thanks guys,
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect String.compareTo works as much from luck as anything else
 
reply
    Bookmark Topic Watch Topic
  • New Topic