C语言位打印输出
1 // 打印辅助函数,用于显示位状态 2 void printBits(uint32 value, uint8 bits) 3 { 4 int16 i; 5 uint8 bitNum,cnt; 6 7 bitNum = ((bits+7)/8)*8; 8 cnt = 0; 9 10 // print bits 11 printf("0x%X = 0b ", value); 12 for (i = bitNum - 1; i >= 0; --i) 13 { 14 printf("%d", (value >> i) & 1); 15 // print space 16 cnt++; 17 if((cnt%4) == 0) 18 { 19 printf(" "); 20 } 21 } 22 printf("\n"); 23 }
1 输出示例: 2 0x4 = 0b 0000 0100 3 0x10 = 0b 0000 0000 0001 0000 4 0x10000 = 0b 0000 0000 0000 0001 0000 0000 0000 0000 5 0xFB = 0b 1111 1011 6 0xFFEF = 0b 1111 1111 1110 1111 7 0xFFFEFFFF = 0b 1111 1111 1111 1110 1111 1111 1111 1111