1.模除實現: #include<stdio.h> int main() { int m, n, i, x, y ,count=0; printf("請輸入m,n:"); scanf_s("%d%d", &m, &n); for (i = 31; i >= 0; i--) { x = m % 2; m = m / 2; y = n % 2; n = n / 2; if (x != y) //通過循環比較每一位是否相等 { count++; } } printf("不同的bit位有%d個\n",count); system("pause"); return 0; } 2.移位運算實現: #include<stdio.h> int main() { int m, n; int i=32,count=0; printf("請輸入m,n:"); scanf_s("%d%d", &m, &n); int j = 0; while (i) { int a, b; a = ((m >>j)& 1); //右移一位和1相與,再比較 b = ((n >>j)& 1); if (a != b) { count++; } j++; i--; } printf("不同的bit位有%d個\n", count); system("pause"); }