fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. const int a = 4;
  6. const int b = 3;
  7.  
  8. string to_bits(int a)
  9. {
  10. string res="";
  11. for(;a!=0;a=a>>1) res = ((a&1)==1?"1":"0") + res;
  12. return res;
  13. }
  14.  
  15. unsigned int num_bits(const int a, const int b)
  16. {
  17. unsigned int res=0;
  18. auto c = a ^ b;
  19.  
  20. for(auto c=a^b; c!=0; c=c >> 1) res += ( (c&1)==1?1:0 );
  21.  
  22. return res;
  23. }
  24.  
  25. int main() {
  26. // your code goes here
  27. cout << "a=" << a <<" = " << to_bits(a) << ", b=" << b << " = " << to_bits(b) << " --> " << num_bits(a, b) << endl;
  28. return 0;
  29. }
  30.  
  31.  
Success #stdin #stdout 0s 4460KB
stdin
Standard input is empty
stdout
a=4 = 100, b=3 = 11 --> 3