Endiannes & Splitting Values

Gary Willoughby via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 7 05:53:29 PDT 2016


On Thursday, 7 July 2016 at 10:48:56 UTC, Lodovico Giaretta wrote:
> On Thursday, 7 July 2016 at 10:45:12 UTC, Gary Willoughby wrote:
>> On Thursday, 7 July 2016 at 08:21:53 UTC, Lodovico Giaretta 
>> wrote:
>>> Are you sure that this works in both big-endian and 
>>> little-endian systems?
>>
>> It shouldn't matter. You're just interested in the high and 
>> low 4 byte chunks (which are to be interpreted as an int) 
>> which will return in the relevant endianess of your machine.
>
> But are the high 4 bytes the first 4 or the second 4? It 
> depends on endianness. So your high and low variables may be 
> switched, if I understand correctly.

Ah, I see. You could modify it like this:

union Value
{
     ulong full;

     static struct Bits
     {
         version (BigEndian)
         {
             uint high;
             uint low;
         }
         else
         {
             uint low;
             uint high;
         }

     }

     Bits bits;
     alias bits this;

     this(ulong value)
     {
         this.full = value;
     }
}


More information about the Digitalmars-d-learn mailing list