This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

I am very green in Java(but like it allot) have a bit of experience in VB and help from anyone will be highly appreciated.
I have been pulling out my hair reading forum after forum to try and understand or solve my problem. In short I need to take any file content and mathematically manipulate it for example to get the square root of the file be it in binary or otherwise to be used in a small maths program.
I have played around with the code below and many other ones but just to give you a simple idea what I'm looking for see the code below. I know it is wrong but that is the idea more or less.

What you mean by "I need to take any file content and mathematically manipulate it for example to get the square root of the file be it in binary or otherwise". If you were just considering text files then I can imagine that you maybe want to evaluate the content as an expression but, even though I have a fairly strong mathematical background, but I can't think of an obvious mathematical operation on the content of a binary file.

Welcome to the Ranch.
Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.

Like Richard I have no idea what you are trying to do or even why you are trying to do it. It would help if you could explain what the problem is that you are trying to solve.

Dean Janse Van Rensburg
Greenhorn

Joined: Aug 25, 2013
Posts: 9

posted

0

Hi Richard,

I guess what I'm trying to find out is for example if one take the number 123456789 and get the square root of it it equals 11111.111060555555
My question is can one do that with binary for example 1=110001, 2=110010, 3=110011, 4=110100, 5=110101, 6=110110, 7=110111, 8=111000, 9=111001.
Add them all together in one string "110001110010110011110100110101110110110111111000111001" then get the square root of it even if it leaves one with a floating number. I'm not really interested in the "res" just need to know how many X*X go in to "110001110010110011110100110101110110110111111000111001" without converting each 1's and 0's to a decimal value which will increase space.
Just looking at the answer and converting it directly without res it should look like this 110001110001110001110001110001(11111 in decimal). But how can I do this with Java.
Thank you for your help.

So do I take it that by 'binary' file you mean a file containing only the characters '0' and '1' ? If so then that is not what I and most people round here would consider to be a binary file; it is simply a file that contains only the characters '0' and '1' but it is still a text file.

Other than having this set as a school exercise I can think of no obvious reason to perform this type of manipulation on a file content. Could you explain the reason you think you need to do this then maybe someone could provide a better solution to your problem.

Having said that, here is a class BigInteger (check the Javadoc) that has a constructor that takes a String and a radix so you can convert your text file containing just the characters '1' and '0' to a String and then construct a BigInteger using radix 2. You can add, multiply etc BigInteger values so maybe this will help but without knowing why you want to do these obscure operations it is difficult to propose anything else.

P.S. What happens if your 'binary' file contains millions of '1' and '0' ? Pretty much all arithmetic operations are going to be very very slow.

Dean Janse Van Rensburg
Greenhorn

Joined: Aug 25, 2013
Posts: 9

posted

0

Richard Tookey wrote:So do I take it that by 'binary' file you mean a file containing only the characters '0' and '1' ? If so then that is not what I and most people round here would consider to be a binary file; it is simply a file that contains only the characters '0' and '1' but it is still a text file.

Sorry, no I don't mean that. If I may try another way to explain it:

If I was a computer I would read a file(any file) like this b100101010000000111100100.......(right?) then I take this string of bits, get the square root of it ie Math.sqrt(b1001010100000001111001000101.......) = b101000(not true reflection just for example). I just need to know if one can perform square root or cube root calculations on the raw binary string of bits and if so how does one do it in Java.

Richard Tookey wrote:Other than having this set as a school exercise I can think of no obvious reason to perform this type of manipulation on a file content.

Yes it is a college 2D and 3D project using 1's and 0's but must be from a actual file(small or very big).

Richard Tookey wrote:P.S. What happens if your 'binary' file contains millions of '1' and '0' ? Pretty much all arithmetic operations are going to be very very slow.

Correct that is why I would like to know if it can be done on the raw code of the file. Hope I make sense.
Apology if I take up too much of your time and thank you for your input it has helped to clear up a couple of things for me already.

Yes it can be done. Read the file into a byte array and then use the byte array to create a BigInteger. The methods available on BigInteger allow one to perform limited arithmetic operations and square root can be obtained (using these methods) by implementing one of the standard square root algorithms. Whether or not this approach is what your professor was thinking of is anybody's guess !

<edit>Comment about timescale removed at the request of the moderators since it seems to have offended the moderators more than the OP.</edit>

Dean Janse Van Rensburg
Greenhorn

Joined: Aug 25, 2013
Posts: 9

posted

0

Thank you very much Richard I will start working on that. Can the same be done with cube root as well?