用另外一种思路给你写了段代码,这个效率应该比前面那个逐位判断的方法要快很多!
程序代码:
#include<stdio.h> typedef unsigned char BYTE; typedef struct //bit型位结构体 { unsigned char bit0:1; unsigned char bit1:1; unsigned char bit2:1; unsigned char bit3:1; unsigned char bit4:1; unsigned char bit5:1; unsigned char bit6:1; unsigned char bit7:1; }bit_array; /*............显示传入参数的二进制形式.........*/ void byte_change(BYTE* str, unsigned int len) { BYTE i=0; bit_array *temp=NULL; temp=(bit_array *)str; //强制转换类型 while(i<len) { printf("%d%d%d%d%d%d%d%d ", (temp+i)->bit7, (temp+i)->bit6, (temp+i)->bit5, (temp+i)->bit4, (temp+i)->bit3, (temp+i)->bit2, (temp+i)->bit1, (temp+i)->bit0); i++; } } int main(void) { BYTE array[]={8,7,6,5,4,3,2,1}; //代转换的数组 byte_change(array,sizeof(array)/sizeof(array[0])); printf("\n"); return 0; }
http://blog./csynyk