寫作業這題看了很久都不會@@ 拜託會的人教我 謝謝>< 設下列各運算式中,a的初值為12,b的初值為6,試計算出下列各式c之輸出結果,並撰寫完整的程式碼來驗證之: 甲、 c=a<<1 乙、 c=a>>1 丙、 c=a & b 丁、 c=a ^ b 戊、 c=a | b?

2015-10-28 9:25 am

回答 (1)

2015-10-29 10:00 am
#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;
}


收錄日期: 2021-05-04 02:03:59
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20151028012522AAZpQ24

檢視 Wayback Machine 備份