| | |
hex string to true binary w. bit manipulation
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I need to get a string from the console that is given in hex form like this: 3e900000.
Then I need to convert it into bit so I can do bit wise thigns too it to generate the following out put:
The above is a factored way of showing IEEE 754 for 32bit floating point decimal numbers.
I know that I can use
I can do the problem but I'm stuck on being able to easily get a real form of 32 bits of binary. Im not allowed to use floating point variables in any of the intermediate steps. I also need to avoid the use of Strings where I can, avoid arrays(for temp storage) and avoid Math.pow().
The point of the problem is not to deal with java or how to convert stirngs to what ever but the binary manipulation using bitwise functions and bit shifts. Like I said, I can do the bits part, Im just stuck on hex string to real full 32 bit binary no trimming leading 0 or stuff like that. it has to be in a form that can be bit masked.
Then I need to convert it into bit so I can do bit wise thigns too it to generate the following out put:
•
•
•
•
3e900000 ==> 2^-2(1 + 2^-3)
I know that I can use
String args[] to get the console input. So I would be using args[0] . No need for error checking on this one.I can do the problem but I'm stuck on being able to easily get a real form of 32 bits of binary. Im not allowed to use floating point variables in any of the intermediate steps. I also need to avoid the use of Strings where I can, avoid arrays(for temp storage) and avoid Math.pow().
The point of the problem is not to deal with java or how to convert stirngs to what ever but the binary manipulation using bitwise functions and bit shifts. Like I said, I can do the bits part, Im just stuck on hex string to real full 32 bit binary no trimming leading 0 or stuff like that. it has to be in a form that can be bit masked.
Last edited by mrynit; Jan 23rd, 2008 at 9:14 am.
Java Syntax (Toggle Plain Text)
Float.parseFloat(String value, int radix);
See the API docs.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
I don't know what I was thinking of, I meant Long, of course. ;-)
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
here is the code. enter a valid 8 digit hex IEEE 754 string on commandline and you get the factored version in terms of powers of base two.
java Syntax (Toggle Plain Text)
public class Fexpand { public static void main(String[] args) { long hex = Long.parseLong(args[0], 16); // shift out every thing but the sign bit that is at 32 bit mark. long sign = hex >> 31; // returns 1 if sign bit is 1, else 0. long sign_bit = sign & 1; // shift to gain exponent value. shifting out the fractional part long binary_exponate = hex >> 23; // bit mask removes sign bit // 011111111 long no_sign_bit_exponate = binary_exponate & 255; long exponent = no_sign_bit_exponate - 127; if (sign_bit == 1) System.out.print("-"); System.out.print( args[0] + " ==> 2^" + exponent + "( 1 "); // bit mask 11111111111111111111111 // test to see if there is any farctional part. if ((hex & 8388607) != 0) { for (int i = 1; i < 24; i++) { // bitmask 10000000000000000000000 if ((hex & 4194304) == 4194304) { System.out.print("+ 2^-" + i + " "); } hex = hex << 1; } } System.out.println(")"); } }
![]() |
Other Threads in the Java Forum
- Previous Thread: External packages and import ?
- Next Thread: How to call Jar file in ASP Application ?
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






