Results 1 to 7 of 7
  1. #1
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default extracting bits from big numbers

    Hi all,

    what i want to do it this

    1. read a file containing big numbers as a list (one in each line). the numbers are big .. reaching upto 10 digits

    2. read the file line by line -> get the number as a string

    3. extract m lower order bits from the number

    now i have done steps 1 and 2 but for the 3rd one im not sure how to go about it ... any help will be much appreciated.

    regards,
    ankit

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I assume you're storing them as longs. To get at lower bits you AND the number with a bit mask. For example
    Java Code:
    n & 0x0003
    will return the lowest 2 bits of x (0x3 is 0011 in binary).
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    VanGo is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    First thing, I'm probably to new to be answering questions about Java but I'll try.

    You said the number your working with is in string form.

    Could you use string commands to get what you want.

    Like

    trim() // to trim off any leading spaces or trailing spaces
    length() // to find the length of your string
    substring( ) // then extract the end characters, actaully any part of the string you want

    I've done this many times in other languages, just once in Java and I don't have an example handy.
    Last edited by VanGo; 04-18-2009 at 02:59 PM.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    If the number is too big for a long, then parse it into a BigInteger. You can then use String.format with the "%x" conversion to get the hexadecimal string, which you can then use the String methods above to cut it down to a manageable size (decimal format won't line up with binary bit boundaries).
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    There are perhaps some constricts on the problem solution that haven't been stated. Let me make comments on the obvious solutions using strings and using numerical methods.

    If the value is a string, the suggestion has been to simply read the string and extract the last m digits using substring methods, or brew your own using numstr.length, numstr.charAt(ndx), and other string methods to build the "last m" string. Avoids the need to use numerical classes and the conversion manipulations that would require. Quick and simple. Only problem arises if you have to handle the last 5 digits of 10 places behind the decimal. Not part of the problem statement.

    You shouldn't have to use masks or hexadecimal notation. OrangeDog has pointed out that binary notation (power of 2) doesn't align to base 10. There are bit shifting algorithms to convert binary to bcd, but suspect that is outside the scope of what you want, need, or would be expected to do.

    If you have to use numerical methods, Input scanning/format methods should read a long. Then use mod; eg. num = dd1 *10^n + dd2*10^(n-1) + ... + ddx*10^m + ... should be enough of a hint. I'm guessing you don't need BigInteger, but that also has a mod method.

  6. #6
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default

    thank you to everyone who replied.

    I ended up converting the string to BigInteger using the bigInteger constructor and then using the & operator to get the bits i wanted.

    -ankit

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    To other repliers: read the question. The OP wanted to extract bits, not digits.

    The canonical method is bit masks for lower-order and bit shifts for higher-order. This also happens to be the simplest in terms of both Java and machine code as well as the fastest method.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. [SOLVED] Extracting Tokens Help
    By Hunkpapa in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 01:17 AM
  2. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  3. Extracting JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:17 AM
  4. Extracting embedded files
    By kasturi in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 12:25 PM
  5. Extracting data from an XML file...
    By techno_brains in forum New To Java
    Replies: 1
    Last Post: 07-15-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •