Skip to main content
GameDev.net gamedev.net
🔒 Locked

Java help, binary program

Started by Lothia Jan 24, 2008 at 3:05 PM 5 replies 800+ views
Original Post
Lothia
Lothia
I am having trouble with a program I am suppose to make. The simple issue is, all ways I would do it I am not allowed to. I am suppose to take a hex number and transfer it into the exponent times the sum of powers of 2. I cannot figure out how to get this into a usable integer. I have done this int hex = Integer.parseInt("3e900000", 16); String b = Integer.toBinaryString(hex); but that just brings it t a string and as far as I know I cannot do anything with a string. Does any one have any suggestions? Here is the exact assignment so u will see what things I cannot do which I would do.
Quote:
Write a program that takes a hexadecimal number which it interprets to be a IEEE single precision floating point value and prints it out as 2 to the exponent times the sum of powers of 2. The input value should be a command line parameter. Use of the program should look like this: F:\371\lab1> java Fexpand 3e900000 3e900000 ==> 2^-2(1 + 2^-3) Use only integer (bitwise and shift) operations. Do not use floating point variables in any of the intermediate steps. Do not use Strings where you can avoid it. Avoid arrays. Avoid Math.pow(...). For this lab, no writeup is required. Hand this lab in by emailing your code to me (so I can run it).
Any help would be a welcome addition to this confused man.
Antheus
Antheus
Quote:
I cannot figure out how to get this into a usable integer


Your "int hex" is all you need. Those are the bits representing your floating point number.

See here. These are the 32-bits you're given (hex number in your case).

Now go through what those bits represent, extract them, then figure out a formula to give you the result required by assignment.

What you need to do is approximately outlined in "A more complex example".
Lothia
Lothia
k k, well I understand how to do the IEEE but I cannot figure a way in java. I was able to get the S and exponent. But the fraction is a bit harder especially when java doesn't like to show leading zeros. I wanted to be able to do the opposite of a bit operator that will take off from the top but there seems to be no command like that. I got it down to aprox 1 with 20 zeros behind it and then 2 leading zeros. (there is only one 1 in it). So my main problem is figuring out how to count where the 1's are. (If there were more then 1 of them).
IE currently I have 0 01111101 001000000000000000000
and I got the first two ares, and the last one displays as 1000000000000000000, and I need that to be the value of 2^-3, but yeah.
Zahlman
Zahlman
Quote:
Original post by Lothia
IE currently I have 0 01111101 001000000000000000000
and I got the first two ares, and the last one displays as 1000000000000000000, and I need that to be the value of 2^-3, but yeah.


We have the bits '001000000000000000000',

and we want to get a textual result of "(1 + 2^-3)".

So, we must understand how those bits correspond to the text output.

We will always have "(1" at the front, except for "denormalized" numbers.

The reason we put " + 2^-3" in our result is that the 3rd bit in the bit pattern '001000000000000000000' is set. (This should be obvious)

So, look at each bit in turn: if it is set, append an appropriate bit of text to your string. The first bit is 0, so we don't append " + 2^-1". The second bit is 0, so we don't append " + 2^-2". The third bit is 1, so we do append " + 2^-3". And so on. Now, be careful: By "first" bit, which bit position in the int do we really mean? And "second", etc.?

(And don't forget the closing bracket :) )

Of course, the prof wants you to "avoid Strings". That doesn't mean to use any weirder tricks to put together the text in memory ;) It simply means to output each piece of it, as you figure it out. You will never need to go back and correct anything.
Antheus
Antheus
Quote:
Of course, the prof wants you to "avoid Strings"


The exercise is quite specific what it's really about:
Quote:
Use only integer (bitwise and shift) operations


Floats are just a side dish, so are strings.
Lothia
Lothia
Opps I accidentally used Strings lol. The only way I could figure out how to do it, was use a for loop going through 23 times and each time right shift one, & that against 0x0000001 and see if the two are equal. So I checked mine backwards.
I know the assignment is straightforward, but when you are in a class that does not involve Java any amount not a single minute is spent in teaching the binary parts of it and expected to do something that I have no idea what it is other then when doing it on paper, its a bit confusing. It's a hardware class which will go into C towards the end but yeah. Thanks for the help all =)
Zahlman
Zahlman
It sounds horrible (totally against the language philosophy and all), but Java is actually a good environment for learning really low-level stuff in - you simply have to ignore all the high-level stuff that is available to you. The reason is simple: the JVM makes guarantees about endianness, data sizes, memory organization, FP and signed integer representation, etc. so you *know* what "machine" you're working with. With C or C++ (or assembly language), your code is specific to the machine you're working on, so you have to be sure you're on a machine that's compatible with the assumptions of the assignment.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.