"Union" struct type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yair

    #1

    "Union" struct type

    Hi,
    I need to shift the bits of a float type.
    In order to do so, I declared the following struct:

    [StructLayout(La youtKind.Explic it, Size=4)]
    private struct unionIntFloatTy pe
    {
    [FieldOffset(0)]
    public float m_asFloat;

    [FieldOffset(0)]
    public int m_asInt;
    }

    Problem is: If I initialize the struct via 'm_asFloat',
    and then extract the data using 'm_asInt', I get
    CS0170: "Use of possibly unassigned field..." (Since
    actually m_asInt was not initialized directly).
    Writing a dummy 'm_asInt=0' at the begining of the call is
    quite costly, since I'm within a loop.

    Will appreciate any help with this...
  • Jon Skeet

    #2
    Re: "Union&quo t; struct type

    Yair <NoMail@NoMail. com> wrote:[color=blue]
    > Thanks for your reply.
    > The reason I'm avoiding 'BitConverter' is that it is
    > relatively slow (about x4 times than the union thing).[/color]

    Interesting. (I presume you know for sure that you absolutely need the
    performance?)

    I wonder why BitConverter does what it does instead of using the union
    idea internally... if there's a good reason, it might be worth knowing
    to see whether it's relevant for your code as well.

    One thing you could do for your struct is to give it a constructor or
    two, which do the "dummy" assignment for you. For instance, you could
    code it as:

    using System;
    using System.Runtime. InteropServices ;

    public class Test
    {
    static void Main()
    {
    IntFloatConvert er x = new IntFloatConvert er(4);
    Console.WriteLi ne (x.FloatValue);
    Console.WriteLi ne ((float)x);

    IntFloatConvert er y = (IntFloatConver ter) 3.5d;
    Console.WriteLi ne (y.IntValue);
    Console.WriteLi ne ((int)y);
    }
    }

    [StructLayout(La youtKind.Explic it, Size=4)]
    public struct IntFloatConvert er
    {
    [FieldOffset(0)]
    int intValue;

    public int IntValue
    {
    get { return intValue; }
    set { intValue = value; }
    }

    [FieldOffset(0)]
    float floatValue;

    public float FloatValue
    {
    get { return floatValue; }
    set { floatValue = value; }
    }


    public IntFloatConvert er (int i)
    {
    floatValue=0;
    intValue = i;
    }

    public IntFloatConvert er (float f)
    {
    intValue=0;
    floatValue = f;
    }


    public static explicit operator int (IntFloatConver ter x)
    {
    return x.intValue;
    }

    public static explicit operator float (IntFloatConver ter x)
    {
    return x.floatValue;
    }

    public static explicit operator IntFloatConvert er (int i)
    {
    return new IntFloatConvert er(i);
    }

    public static explicit operator IntFloatConvert er (float f)
    {
    return new IntFloatConvert er(f);
    }
    }

    The code above gives both casting and property access to let you easily
    convert from one form to the other.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Jon Skeet

      #3
      Re: &quot;Union&quo t; struct type

      Jon Skeet <skeet@pobox.co m> wrote:[color=blue]
      > IntFloatConvert er y = (IntFloatConver ter) 3.5d;[/color]

      This line should, of course, have 3.5f, not 3.5d. Not that the compiler
      complained, interestingly.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Jeff Lindholm

        #4
        Re: &quot;Union&quo t; struct type

        [color=blue]
        > [StructLayout(La youtKind.Explic it, Size=4)]
        > private struct unionIntFloatTy pe
        > {
        > [FieldOffset(0)]
        > public float m_asFloat;
        >
        > [FieldOffset(0)]
        > public int m_asInt;
        > }
        >
        > Problem is: If I initialize the struct via 'm_asFloat',
        > and then extract the data using 'm_asInt', I get
        > CS0170: "Use of possibly unassigned field..." (Since
        > actually m_asInt was not initialized directly).[/color]

        unionIntFloatTy pe i = new unionIntFloatTy pe();

        i.m_asFloat = 1.0f;

        int foo = i.m_asInt;



        Works fine for me. I get no error etc. I am using 1.1 of the framework.


        Comment

        • Val Savvateev

          #5
          Re: &quot;Union&quo t; struct type

          You could add a "getter" property for asInt field and use it to get the
          integer value instead of accessing the asInt directly.

          "Yair" <NoMail@NoMail. com> wrote in message
          news:8de401c35b 61$353f1db0$a00 1280a@phx.gbl.. .[color=blue]
          > Hi,
          > I need to shift the bits of a float type.
          > In order to do so, I declared the following struct:
          >
          > [StructLayout(La youtKind.Explic it, Size=4)]
          > private struct unionIntFloatTy pe
          > {
          > [FieldOffset(0)]
          > public float m_asFloat;
          >
          > [FieldOffset(0)]
          > public int m_asInt;
          > }
          >
          > Problem is: If I initialize the struct via 'm_asFloat',
          > and then extract the data using 'm_asInt', I get
          > CS0170: "Use of possibly unassigned field..." (Since
          > actually m_asInt was not initialized directly).
          > Writing a dummy 'm_asInt=0' at the begining of the call is
          > quite costly, since I'm within a loop.
          >
          > Will appreciate any help with this...[/color]


          Comment

          Working...