unsigned char reverse8(
unsigned char c )
{
c = ( c & 0x55 ) << 1 | ( c & 0xAA )
>> 1;
c = ( c & 0x33 ) << 2 | ( c & 0xCC ) >>
2;
c = ( c & 0x0F ) << 4 | ( c & 0xF0 ) >> 4;
return c;
}
unsigned long
func(unsigned long x)
{
x = (x & 0x55555555UL) + ((x >> 1)
& 0x55555555UL);
x = (x & 0x33333333UL) + ((x >> 2) &
0x33333333UL);
x = (x & 0x0f0f0f0fUL) + ((x >> 4) &
0x0f0f0f0fUL);
x = (x & 0x00ff00ffUL) + ((x >> 8) &
0x00ff00ffUL);
x = (x & 0x0000ffffUL) + ((x >> 16) &
0x0000ffffUL);
return x;
} 更佳的算法见“HAKMEM算法”
以字节为单位反转
inline
DWORD Reverse4( DWORD n )
{
DWORD m = ((n>>8)&0x00FF00FF) |
((n&0x00FF00FF)<<8);
return ((m>>16)&0x0000FFFF) |
((m&0x0000FFFF)<<16);
}
汇编 BSWAP 指令
unsigned long
_byteswap_ulong( unsigned long val );
uint32_t bswap_32( uint32_t x );
阅读(1415) | 评论(4) | 转发(0) |