hex string to true binary w. bit manipulation

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 15
Reputation: mrynit is an unknown quantity at this point 
Solved Threads: 0
mrynit's Avatar
mrynit mrynit is offline Offline
Newbie Poster

hex string to true binary w. bit manipulation

 
0
  #1
Jan 23rd, 2008
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:
3e900000 ==> 2^-2(1 + 2^-3)
The above is a factored way of showing IEEE 754 for 32bit floating point decimal numbers.

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: hex string to true binary w. bit manipulation

 
0
  #2
Jan 23rd, 2008
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 15
Reputation: mrynit is an unknown quantity at this point 
Solved Threads: 0
mrynit's Avatar
mrynit mrynit is offline Offline
Newbie Poster

Re: hex string to true binary w. bit manipulation

 
0
  #3
Jan 24th, 2008
i used Long.parseLong(String value, int radix);
there is not Float.parseFloat(String value, int radix);

I seem to have a hard time searching the API to find what I need.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: hex string to true binary w. bit manipulation

 
0
  #4
Jan 24th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 15
Reputation: mrynit is an unknown quantity at this point 
Solved Threads: 0
mrynit's Avatar
mrynit mrynit is offline Offline
Newbie Poster

Re: hex string to true binary w. bit manipulation

 
0
  #5
Jan 25th, 2008
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.

  1. public class Fexpand
  2. {
  3. public static void main(String[] args)
  4. {
  5. long hex = Long.parseLong(args[0], 16);
  6.  
  7. // shift out every thing but the sign bit that is at 32 bit mark.
  8. long sign = hex >> 31;
  9.  
  10. // returns 1 if sign bit is 1, else 0.
  11. long sign_bit = sign & 1;
  12.  
  13.  
  14. // shift to gain exponent value. shifting out the fractional part
  15. long binary_exponate = hex >> 23;
  16.  
  17. // bit mask removes sign bit
  18. // 011111111
  19. long no_sign_bit_exponate = binary_exponate & 255;
  20.  
  21.  
  22. long exponent = no_sign_bit_exponate - 127;
  23.  
  24. if (sign_bit == 1)
  25. System.out.print("-");
  26.  
  27. System.out.print( args[0] + " ==> 2^" + exponent + "( 1 ");
  28.  
  29. // bit mask 11111111111111111111111
  30. // test to see if there is any farctional part.
  31. if ((hex & 8388607) != 0)
  32. {
  33.  
  34. for (int i = 1; i < 24; i++)
  35. {
  36. // bitmask 10000000000000000000000
  37. if ((hex & 4194304) == 4194304)
  38. {
  39. System.out.print("+ 2^-" + i + " ");
  40. }
  41. hex = hex << 1;
  42. }
  43.  
  44. }
  45. System.out.println(")");
  46. }
  47. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC