469,917 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

"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(LayoutKind.Explicit, Size=4)]
private struct unionIntFloatType
{
[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...
Nov 15 '05 #1
4 5748
Yair <No****@NoMail.com> wrote:
Thanks for your reply.
The reason I'm avoiding 'BitConverter' is that it is
relatively slow (about x4 times than the union thing).


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()
{
IntFloatConverter x = new IntFloatConverter(4);
Console.WriteLine (x.FloatValue);
Console.WriteLine ((float)x);

IntFloatConverter y = (IntFloatConverter) 3.5d;
Console.WriteLine (y.IntValue);
Console.WriteLine ((int)y);
}
}

[StructLayout(LayoutKind.Explicit, Size=4)]
public struct IntFloatConverter
{
[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 IntFloatConverter (int i)
{
floatValue=0;
intValue = i;
}

public IntFloatConverter (float f)
{
intValue=0;
floatValue = f;
}
public static explicit operator int (IntFloatConverter x)
{
return x.intValue;
}

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

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

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

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jon Skeet <sk***@pobox.com> wrote:
IntFloatConverter y = (IntFloatConverter) 3.5d;


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
[StructLayout(LayoutKind.Explicit, Size=4)]
private struct unionIntFloatType
{
[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).


unionIntFloatType i = new unionIntFloatType();

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.
Nov 15 '05 #4
You could add a "getter" property for asInt field and use it to get the
integer value instead of accessing the asInt directly.

"Yair" <No****@NoMail.com> wrote in message
news:8d****************************@phx.gbl...
Hi,
I need to shift the bits of a float type.
In order to do so, I declared the following struct:

[StructLayout(LayoutKind.Explicit, Size=4)]
private struct unionIntFloatType
{
[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...

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Marek Lewczyk | last post: by
2 posts views Thread by thomasfj | last post: by
reply views Thread by Codex Twin | last post: by
6 posts views Thread by Bob Altman | last post: by
1 post views Thread by benn686 | last post: by
7 posts views Thread by php_mysql_beginer911 | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.