[不会]位运算不懂例子

我的梦境 提交于 2020-01-16 13:23:24

 

 

 1 #include<iostream>
 2 using namespace std;
 3 int main()  {
 4     int a,b;
 5     int n;
 6 while(cin >> n)    {
 7         cin >> a;
 8         for(int i=1;i<n;i++)  {
 9             cin >> b;
10             a^=b;
11         }
12         cout << a << endl;
13     }
14     return 0;
15 }

 

 

二进制的遍历:

 

 

 

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int a[5] = {1,3,5,6,8};
 6     int n;
 7     n = 5;
 8     for(int i = 0;i < (1 << n); i++) {//从0~2^n-1个状态,遍历每一个集合
 9         for(int j = 0; j < n; j++) { //遍历二进制的每一位
10             if(i & (1 << j)){   //判断二进制第j位是否存在
11                 cout << a[j] << ' ';
12             }
13         }
14         cout << endl;
15     }
16     return 0;
17  } 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!