This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Initialize long with floats binary representation

Hello,

how can I achieve


long l = WHAT_EVER_MACRO (-12.5);

main()
{
  printf("%X",l);
}

resulting in output "0xC1480000" the binary representation of the float -12.5 .
Without that MACRO the value is 0xFFFFFFF4 = -12 for integers .

Because of given circumstances in an existing project I have to store float values during initialization in long variables.

Is there any possibility?

Thanks in advance

Jürgen

Parents
  • "But union as type converters are still just one usage alternative."

    sure.

    "So it might be useful to combine all the information into generic methods."

    it "might". I was just objecting to the notion that a union is very often used to share space, based on my experience.

    I didn't say that it is NOT helpful to include type information in any application.

Reply
  • "But union as type converters are still just one usage alternative."

    sure.

    "So it might be useful to combine all the information into generic methods."

    it "might". I was just objecting to the notion that a union is very often used to share space, based on my experience.

    I didn't say that it is NOT helpful to include type information in any application.

Children
  • "I was just objecting to the notion that a union is very often used to share space, based on my experience."

    But that would obviously depend on:
    - how many different types of programs you have looked at.
    - how many different developers who have designed the programs.
    - how many different types of platforms.
    - ...

    The use of unions for polymorphic objects obviously happens more when writing object-oriented programs in C. But object-oriented programs tends to expect object pointers. And the 8051 is lousy with indexing around pointers making it a very expecnsive concept for the 8051 architecture. Lots of other architectures are great at handling pointer+offset addressing but have had to wait lots of years for a [good] C++ compiler.

    "I didn't say that it is NOT helpful to include type information in any application."

    So why did you write
    "and why you think it will help maintain data integrity / validity."
    instead of something like:
    "But in this case, the union is used as a temporary and lives for such a short time, that there is no need to flag it as used or what type that was originally assigned"?

  • "But that would obviously depend on:"

    sure. but why is it relevant here?

    "So why did you write
    "and why you think it will help maintain data integrity / validity.""

    because I presumed that we were talking about THIS PARTICULAR case in this very discussion, not ALL cases.

    maybe that's an assumption not applicable to you guys?