Byte Order Swapping Function
Piotr Szturmaj
bncrbme at jadamspam.pl
Sun Jul 17 02:25:23 PDT 2011
Jonathan M Davis wrote:
> private T swapEndianImpl(T)(T val)
> if(is(Unqual!T == ulong))
> {
> return ((val& 0xff00000000000000UL)>> 56) |
> ((val& 0x00ff000000000000UL)>> 40) |
> ((val& 0x0000ff0000000000UL)>> 24) |
> ((val& 0x000000ff00000000UL)>> 8) |
> ((val& 0x00000000ff000000UL)<< 8) |
> ((val& 0x0000000000ff0000UL)<< 24) |
> ((val& 0x000000000000ff00UL)<< 40) |
> ((val& 0x00000000000000ffUL)<< 56);
> }
Here's shorter version that I use:
@safe pure nothrow ulong bswap(ulong i)
{
i = (i >> 32) | (i << 32);
i = ((i & 0xff00ff00ff00ff00) >> 8) | ((i & 0x00ff00ff00ff00ff) << 8);
return ((i & 0xffff0000ffff0000) >> 16) | ((i & 0x0000ffff0000ffff)
<< 16);
}
More information about the Digitalmars-d
mailing list