How can I get/set individual bits in a float?

I have to admit in all my work with Java, I've never come across the need for a Java union (like the C union, not the SQL one) and I cannot find an answer here on SO. Admittedly, most of my work in Java has been at higher abstractions than bit-fiddling.

I have a integer which I'm setting individual bits for and I want to print out the equivalent IEEE754 single-precision float.

In C, I'd do something like:

union { int i; float f; } x; x.i = 0x27; printf ("%f\n", x.f);

How do I do a similar thing in Java? Is it even possible to treat the same memory as two different data types in Java?

I did search on both SO and elsewhere for "java union" but it swamped me with SQL stuff - I couldn't find a way to do this.

-------------Problems Reply------------

I have a integer which I'm setting individual bits for and I want to print out the equivalent IEEE754 single-precision float.

Use intBitsToFloat() for that.

Substitutes for Missing C Constructs

The designers of the Java programming language chose to omit the union construct because there is a much better mechanism for defining a single data type capable of representing objects of various types: subtyping. A discriminated union is really just a pallid imitation of a class hierarchy.

Includes examples.

Java doesn't provide any way to treat a value as another value on raw bit level - no unions, nothing like reinterpret_cast. However, for your particular case of treating float as int and vice versa, you can use java.lang.Float.floatToIntBits and intBitsToFloat methods.

Some useful articles & faq to this subject are,

Union types in Java?

Substitutes for Missing C Constructs

If you are interested in accessing the bit-level representation of a float, java.lang.Double contains method to do this (doubleToLongBits etc.)

Union'ing between pointer types (or between pointers and their numeric represetantion) would open holes in the type system, so it is strictly impossible.

Category:java Views:0 Time:2009-08-21

Related post

  • Extracting individual digits from a float 2008-12-03

    I have been banging my head on this one all day. The C++ project I am currently working on has a requirement to display an editable value. The currently selected digit displays the incremented value above and decremented value below for said digit. I

  • Center individual blocks that may wrap 2012-01-27

    I have a group of blocks that are not floated, but that have a set width and height and display inline-block. Ideally, I would like for this entire block to be centered in the viewport. See: The problem is that even if I

  • How deterministic is floating point inaccuracy? 2008-11-30

    I understand that floating point calculations have accuracy issues and there are plenty of questions explaining why. My question is if I run the same calculation twice, can I always rely on it to produce the same result? What factors might affect thi

  • Drawing arrows on an HTML page to visualize semantic links between textual spans 2009-02-16

    I have an HTML page with some textual spans marked up something like this ... <span id="T2" class="Protein">p50</span> ... <span id="T3" class="Protein">p65</span> ... <span id="T34" ids="T2 T3" class="Positive_regulation"

  • How to remove the margin between two images? 2009-10-29

    I've tried to set the margin and border to 0,but still not working. <style type="text/css"> img {margin:0;} </style> <body> <img src="/static/btnNext.gif" border="0" /> <img src="/static/btnSave.gif" border="0" /> How to

  • transform:scale to fullscreen? 2010-10-25

    Good day all, A series of differently sized images are each individually wrapped in left floating differently sized divs on a horizontally scrolling web page. I want to be able to tap on a div to scale it to fullscreen, (by adding a css class contain

  • Billboarded sprites stop working if moved from 0,0,0 2011-12-12

    I got this code for billboarding I created (With the help of others, and a nehe tutorial), but it seems that when I move my sprites from 0,0,0 it stops working correctly, for example the closer I get to a sprite that's at 50,0,50 the more sideways it

  • Cast to union and weird number format 2012-02-17

    Can anybody tell me how to make this C code work in C++ please? uint64_t flv_dbl2int( double value ) { return (union {double f; uint64_t i;}){value}.i; } I'm not precisely sure what is going on, is it bit shift? --------------Solutions------------- A

  • what is an efficient way to transpose a matrix in a text file? 2012-03-20

    i have a text file that holds a 2-dimensional matrix. it looks like the following. 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 as you can see, each row is delimited by a new line and each column is delimited by a space. i need to tran

  • How do I prevent Ajax elements from wrapping? 2012-04-23

    I have a div in an html page that is injected with 12 divs via Ajax. When I resize the browser window, the elements or their containing div seem to change size, in which the divs begin to wrap in undesirable ways. When the page is 100% size, the divs

  • What are the real benefits of TFS for the individual developer? 2008-09-15

    The question of TFS versus OSS has already been asked, with most answers tending toward OSS as the better alternative. I'm in a position where my employer (an ISV with a mix of onshore, offshore development staff) is about to take the plunge to TFS.

  • How do I branch an individual file in SVN? 2008-09-22

    The subversion concept of branching appears to be focused on creating an [un]stable fork of the entire repository on which to do development. Is there a mechanism for creating branches of individual files? For a use case, think of a common header (*.

  • Developing for different platforms individually, does anyone recommend it? 2008-09-30

    I know it is easy to recommend several cross platform libraries. However, are there benefits to treating each platform individually for your product? Yes there will be some base libraries used in all platforms, but UI and some other things would be d

  • In sharepoint disabling a individual list item from being updated 2008-11-14

    What im wondering how to do is when someone edits a list item and it goes through my event code that is fired when a change is made and save is hit, i dont want anyone to be able to edit that list item itself while its still processnig that request.

  • Web store version control for very small group or individual? 2008-11-21

    For very small teams, or an individual developer, is there a source code control tool which is a web service, or web based application, with no or very little cost? Ideally, it would work with Microsoft development. IDE Integration would be awesome,

  • How can you two-way bind a checkbox to an individual bit of a flags enumeration? 2008-11-28

    For those who like a good WPF binding challenge: I have a nearly functional example of two-way binding a checkbox to an individual bit of a flags enumeration (thanks Ian Oakes, original MSDN post). The problem though is that the binding behaves as if

  • Will ASP.Net remove individual tables from a cached dataset to free up memory? 2008-12-03

    I have a strange, sporadic issue. I have stored procedure that returns back 5 small tables (e.g. IDs and Text Descriptions for Status drop down lists and such). The code calls this and places the returning dataset into ASP.Net cache. Separate methods

  • Preventing sqlplus truncation of column names, without individual column formatting 2008-12-09

    By default sqlplus truncates column names to the length of the underlying data type. Many of the column names in our database are prefixed by the table name, and therefore look identical when truncated. I need to specify select * queries to remote DB

  • cmd : delete is deleting the whole directory instead of individual files 2008-12-16

    I'm running a simple batch file which is generated by a vbscript to delete individual files, however when I execute it, it is deleting entire subdirectories. Anyone have any ideas on this? Below is the batch file. rem 2008-12-15D:\DP-Production\Admin

Copyright (C), All Rights Reserved.

processed in 0.099 (s). 11 q(s)