#include<stdio.h>
#include<stdlib.h>
int to_binary(int);
int main(void)
{
int a=12,b=6;
printf("a\t=%6d\n",a);
printf("\t=%6d\n",to_binary(a));
printf("b\t=%6d\n",b);
printf("\t=%6d\n",to_binary(b));
printf("................................\n");
printf("a<<1\t=%6d\n",a<<1);
printf("\t=%6d\n",to_binary(a<<1));
printf("\t(往左移一位)\n");
printf("................................\n");
printf("a>>1\t=%6d\n",to_binary(a>>1));
printf("\t=%6d\n",a>>1);
printf("\t(往右移一位)\n");
printf("................................\n");
printf("a&b\t=%6d\n",a&b);
printf("\t %6d\n",to_binary(a));
printf("\t %6d\n",to_binary(b));
printf("\t ------\n");
printf("\t %6d\n",to_binary(a&b));
printf("\t(ab皆為1輸出為1)\n");
printf("................................\n");
printf("a^b\t=%6d\n",a^b);
printf("\t %6d\n",to_binary(a));
printf("\t %6d\n",to_binary(b));
printf("\t ------\n");
printf("\t %6d\n",to_binary(a^b));
printf("\t(ab不同則輸出為1)\n");
printf("................................\n");
printf("a|b\t=%6d\n",a|b);
printf("\t %6d\n",to_binary(a));
printf("\t %6d\n",to_binary(b));
printf("\t ------\n");
printf("\t %6d\n",to_binary(a|b));
printf("\t(ab其中一個為1輸出為1)\n");
printf("\n");
system("pause");
return 0;
}
int to_binary(int input)
{
int temp=1,input_binary=0;
while((temp*=2)<=input);
while((temp/=2)>0)
{
input_binary*=10;
input_binary+=((input/temp)%10);
input%=temp;
}
return input_binary;
}