Results 1 to 7 of 7
Thread: extracting bits from big numbers
- 04-18-2009, 07:23 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 22
- Rep Power
- 0
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
- 04-18-2009, 07:34 AM #2
I assume you're storing them as longs. To get at lower bits you AND the number with a bit mask. For example
will return the lowest 2 bits of x (0x3 is 0011 in binary).Java Code:n & 0x0003
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 04-18-2009, 02:47 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
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.
- 04-18-2009, 04:57 PM #4
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
- 04-19-2009, 01:34 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
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.
- 05-05-2009, 04:05 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 22
- Rep Power
- 0
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
- 05-05-2009, 04:36 AM #7
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
-
[SOLVED] Extracting Tokens Help
By Hunkpapa in forum New To JavaReplies: 2Last Post: 11-04-2008, 01:17 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Extracting JAR file
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:17 AM -
Extracting embedded files
By kasturi in forum New To JavaReplies: 0Last Post: 02-07-2008, 12:25 PM -
Extracting data from an XML file...
By techno_brains in forum New To JavaReplies: 1Last Post: 07-15-2007, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks