获取数据中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; }
版权声明:
作者:朝雾之归乡
出处:http://www.cnblogs.com/cnpirate
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。