C实现二进制高低逆序

方法一:效率低

int func(unsigned int uiData , int length)
{
    unsigned int uiValue = 0 ;
    int            i        = 0 ;
    for  ( i = 0 ; i < length ; i++ )  
{
       uiValue  = (uiValue << 1) + (uiData & 0x01) ;
       uiData   = uiData >> 1 ;
    }
    return uiValue ;
}

方法二:分治

int func (unsigned int uiData)
{
    unsigned int uiValue = 0 ;
    /* 分而治之的思想 */
    /* 高16位和低16互换 */
    uiValue = ((uiData >> 16)&0x0000ffff) |
((uiData << 16)&0xffff0000);
/*高低16位中的高低8位互换*/
    uiValue = ((uiValue >> 8)&0x00ff00ff) |
((uiValue << 8)&0xff00ff00);
/*8位中的高低4位互换*/
    uiValue = ((uiValue >> 4)&0x0f0f0f0f) |
((uiValue << 4)&0xf0f0f0f0);
/*4位中的高低2位互换*/
    uiValue = ((uiValue >> 2)&0x33333333) |
((uiValue << 2)&0xcccccccc);
/*2位中的高低位互换*/
    uiValue = ((uiValue >> 1)&0x55555555) |
((uiValue << 1)&0xaaaaaaaa);
    return uiValue ;
}
原文地址:https://www.cnblogs.com/ikaka/p/3355045.html