主题:一个变相的十进制转换成二进制的程序
它应该比一般人所用的连续取模法快.
通过改变a的类型和增加b的成员个数还可以适应更大的十进制值.
union {int a; /*此便程只适用于1Byte以内的数值*/
struct {unsigned int b1:1;
unsigned int b2:1;
unsigned int b3:1;
unsigned int b4:1;
unsigned int b5:1;
unsigned int b6:1;
unsigned int b7:1;
unsigned int b8:1;
}b;
}v;
main()
{printf("Input a decimal number <=255: "); scanf("%d",&v.a);
printf("The binary value is:"%d%d%d%d%d%d%d%d" ,v.b.b8,v.b.b7,v.b.b6,v.b.b5,v.b.b4,v.b.b3,v.b.b2,v.b.b1);
getch(); /*For win2000/xp */
}
通过改变a的类型和增加b的成员个数还可以适应更大的十进制值.
union {int a; /*此便程只适用于1Byte以内的数值*/
struct {unsigned int b1:1;
unsigned int b2:1;
unsigned int b3:1;
unsigned int b4:1;
unsigned int b5:1;
unsigned int b6:1;
unsigned int b7:1;
unsigned int b8:1;
}b;
}v;
main()
{printf("Input a decimal number <=255: "); scanf("%d",&v.a);
printf("The binary value is:"%d%d%d%d%d%d%d%d" ,v.b.b8,v.b.b7,v.b.b6,v.b.b5,v.b.b4,v.b.b3,v.b.b2,v.b.b1);
getch(); /*For win2000/xp */
}