Big Endian and small endian
-
Hello All, Can someone suggest a fast ways to convert big endian to small endina? Thanks! Nachi
there is a bswap ASM instruction, however, this might not be the fastest way on all architectures.
DWORD SwapEnidan(DWORD x)
{
__asm mov eax, x
__asm bswap eax
__asm mov x, eax
return x;
}Another option (that could be better if you have to convert many values and unroll the loop by two) would be splitting into bytes and shifting around:
DWORD x = ...;
DWORD swapped = ((x & 0x000000FF) << 24) |
((x & 0x0000FF00) << 8) |
((x & 0x00FF0000) >> 8) |
((x & 0xFF000000) >> 24));I don't think that XCHG or "manually" swapping bytes is faster.
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen