两个整数 m 和 n 的二进制表达中有多少个位(bit)不同?
数据范围:
import java.util.*; public class Main { private static final int MAX = 1005; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n1 = sc.nextInt(), n2 = sc.nextInt(); int count = 0; for (; n1 != 0 || n2 !=0; n1 >>= 1, n2 >>= 1) { if ((n1 & 1) != (n2 & 1)) { count++; } } System.out.println(count); } }
import java.util.Scanner; /** * @author wylu */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int xor = scanner.nextInt() ^ scanner.nextInt(); int count = 0; while (xor != 0) { xor &= (xor - 1); count++; } System.out.println(count); } } }
#include <stdio.h> #include <stdlib.h> int main(void) { int m, n; fscanf(stdin, "%d %d", &m, &n); return fprintf(stdout, "%d", __builtin_popcount(m ^ n)), 0; }
#include <stdio.h> #include <stdlib.h> int main() { unsigned int x,y; while(scanf("%d %d",&x,&y)!=EOF) { unsigned int temp=x^y; int count=0; while(temp) { if(temp%2) { count++; } temp/=2; } printf("%d\n",count); } return 0; }
import sys def main(): line = sys.stdin.readline() a,b = map(int,line.split()) a = bin(a).replace("0b","") b = bin(b).replace("0b","") n = max(len(a),len(b)) a = a.rjust(n,'0') b = b.rjust(n,'0') count = 0 for i in range(n): if a[i]!=b[i]: count+=1 print(count) if __name__ == '__main__': main()
#include <bits/stdc++.h> using namespace std; int main(){ int m, n; cin >> m >> n; int res = 0; for(int i = 0; i < 32; ++i){ res += ((((1 << i) & m) >> i) ^ (((1 << i) & n) >> i)); } cout << res << endl; return 0; }