获取数据中bit为1的位置和总数

//获取数据中bit为1的位置和总数
int32_t ParseBitOfData(uint32_t uiData, uint8_t usBitTotal, ListData *pRes)
{
    int32_t iRet = 0;
    uint8_t i = 0;
    uint8_t usTotal = 0;
    uint32_t uiTmp = uiData;

    printf("Data:0x%X; Bit:%d.\n", uiData, usBitTotal);

    if (usBitTotal > 32)
    {
        return RET_FAIL;
    }

    for (i=0; i<usBitTotal; i++)
    {
        //printf("%d:0x%X.\n", i, uiTmp);
        if ((uiTmp & 1) == 1)
        {
            pRes->pList[usTotal] = i + 1;
            printf("Index:%d is %d.\n", usTotal, pRes->pList[usTotal]);
            usTotal++;
        }

        uiTmp = uiTmp >> 1;
    }

    pRes->usTotal = usTotal;

    printf("Total is %d.\n", pRes->usTotal);
    

    return RET_OK;

}

 

posted @ 2013-03-04 12:20  朝雾之归乡  阅读(725)  评论(0编辑  收藏  举报