-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbits.cpp
More file actions
34 lines (27 loc) · 721 Bytes
/
bits.cpp
File metadata and controls
34 lines (27 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> v;
v.push_back("apple");
v.push_back("peach");
v.push_back("banana");
v.push_back("mango");
v.push_back("orange");
int n1 = v.size();
cout<<"All subsets using bits"<<endl;
//i goes from 0 to 2^n - 1
for (int i = 0 ; i < (1 << n1); ++i) {
string output("");
// j goes through n bits and checks i for the j th bit set
for (int j = 0 ; j < n1; j++) {
if ( (i & (1<<j)) > 0) {
//cout<<"adding "<<v[j]<<endl;
output += v[j]+" ";
}
}
cout<<output<<endl;
}
cout<<endl;
cout<<"*********************************"<<endl;
}