Thread overview
Union member positions?
Aug 17, 2021
z
Aug 17, 2021
Dennis
Aug 17, 2021
ag0aep6g
August 17, 2021

Is it possible to set a "position" on a union member? or is there is a language-integrated equivalent?
For example, to get access to each byte in an unsigned integer while still supporting the original type.

///a single uint that would be accessed as two ushort, or four separate ubyte
union UnionExample{
uint EAX;

//upper
ushort EAHX;

ubyte EAHH;
ubyte EAHL;

//lower
ushort EALX;

ubyte EALH;
ubyte EALL;
}

Thanks.

August 17, 2021

On Tuesday, 17 August 2021 at 13:46:22 UTC, z wrote:

>

Is it possible to set a "position" on a union member?

You can use anonymous struct and union blocks.

union UnionExample{
    uint EAX;

    struct {
        //upper
        union {
            ushort EAHX;
            struct {
                ubyte EAHH;
                ubyte EAHL;
            }
        }
        //lower
        union {
            ushort EALX;
            struct {
                ubyte EALH;
                ubyte EALL;
            }
        }
    }
}
August 17, 2021
On 17.08.21 15:46, z wrote:
> Is it possible to set a "position" on a union member? or is there is a language-integrated equivalent?
> For example, to get access to each byte in an unsigned integer while still supporting the original type.
> ```D
> ///a single uint that would be accessed as two ushort, or four separate ubyte
> union UnionExample{
> uint EAX;
> 
> //upper
> ushort EAHX;
> 
> ubyte EAHH;
> ubyte EAHL;
> 
> //lower
> ushort EALX;
> 
> ubyte EALH;
> ubyte EALL;
> }
> ```
> Thanks.

union UnionExample
{
    uint EAX;
    struct
    {
        union // upper
        {
            ushort EAHX;
            struct
            {
                ubyte EAHH;
                ubyte EAHL;
            }
        }
        union // lower
        {
            ushort EALX;
            struct
            {
                ubyte EALH;
                ubyte EALL;
            }
        }
    }
}