http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=604&pid=1001

问题描述
给定一个数 N 把统计它的二进制表示里, 1的块数。 如  5 = 101 有两块 1 。 如 11 = 1011 也是有两块 1 
输入描述
第一行输入一个T, 表示有T组数据。 然后有T行, 每行一个整数(N)。
输出描述
对于每组数据,输出一个数表示答案。
输入样例
1
5
输出样例
2

 

 

 

用位运算交换两个数的值(是不是很炫酷?)

#include <iostream>
using namespace std;

void inplace_swap(int *x, int *y)
{
    *y = *x ^ *y;  // Step 1
    *x = *x ^ *y;  // Step 2
    *y = *x ^ *y;  // Step 3
}

int main()
{
    int a = 13, b = 14;
    int * m = &a, *n = &b;
    cout<< *m << '\t' << *n << endl << endl;
    inplace_swap(m, n);
    cout<< *m << '\t' << *n << endl;
    return 0;
}

 

 

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-01
  • 2021-07-07
  • 2022-12-23
  • 2021-08-01
  • 2021-12-23
  • 2022-12-23
相关资源
相似解决方案