标签:
// 位段赋值的例子 #include <stdio.h> int main() { unsigned char puc[4]; struct tagPIM { unsigned char ucPiml; unsigned char ucData0 : 1; unsigned char ucData1 : 2; unsigned char ucData2 : 3; }*pstPimData; pstPimData = (struct tagPIM*)puc; memset(puc, 0, 4);//全部置0 pstPimData->ucPiml = 2;//0000 0010全部取出 pstPimData->ucData0 = 3;//0000 0011取出1 pstPimData->ucData1 = 4;//0000 0100取出00 pstPimData->ucData2 = 5;//0000 0101取出101 // 0000 0000 0000 0000 0000 0000 0000 0000 // 0000 0010 0010 1001 0000 0000 0000 0000 // 0 2 2 9 0 0 0 0 // 结果即 02 09 00 00 printf("%02x %02x %02x %02x\n", puc[0], puc[1], puc[2], puc[3]); return 0; } <img src="http://img.blog.csdn.net/20150511153114682?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
标签:
原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/45644627