Quickest way to change endianness

前端 未结 4 457
一整个雨季
一整个雨季 2021-02-06 17:33

What is the quickest way to reverse the endianness of a 16 bit and 32 bit integer. I usually do something like (this coding was done in Visual Studio in C++):

un         


        
4条回答
  •  再見小時候
    2021-02-06 18:19

    At least in Visual C++, you can use _byteswap_ulong() and friends: http://msdn.microsoft.com/en-us/library/a3140177.aspx

    These functions are treated as intrinsics by the VC++ compiler, and will result in generated code that takes advantage of hardware support when available. With VC++ 10.0 SP1, I see the following generated code for x86:

    return _byteswap_ulong(val);
    
    mov     eax, DWORD PTR _val$[esp-4]
    bswap   eax
    ret     0
    
    return _byteswap_ushort(val);
    
    mov     ax, WORD PTR _val$[esp-4]
    mov     ch, al
    mov     cl, ah
    mov     ax, cx
    ret     0
    

提交回复
热议问题