输出一个整数的每一位。
#include "stdio.h" int main() { int num = 0, count = 0, i = 0; int num1 = 0; printf("请输入一个数:"); scanf("%d",&num); num1 = num; while(num) { num = num/10; count++; } int a[100]; printf("是一个%d位数\n",count); for(i=1 ;i<=count ;i++) { a[i] = (num1)%10; num1 = (num1)/10; } for(i=count ;i>0 ;i--) { printf("%d\t",a[i]); } printf("\n"); return 0; }
两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?
#include "stdio.h" int main() { printf("请输入两个数:"); int a = 0,b = 0,c = 0,count = 0; scanf("%d %d",&a,&b); c = a^b; while(c) { if(c&1 == 1) { count++; } c = c>>1; } printf("这俩数二进制有%d位不同。\n",count); return 0; }