-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepresentSetElements.cpp
More file actions
34 lines (26 loc) · 913 Bytes
/
RepresentSetElements.cpp
File metadata and controls
34 lines (26 loc) · 913 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
/**
* Title : Represent set elements as an n-bit integer
* Author : Tridib Samanta
**/
#include<bits/stdc++.h>
using namespace std;
int main() {
// Let's see how the set {1,3,4,8} can be stored in x
int arr[4] = {1,3,4,8};
// As int is a 32-bit type, an int number can represent any subset of the set {0,1,2,...,31}
int x = 0;
x |= (1 << 1); // x = 00000000 00000000 00000000 00000010
x |= (1 << 3); // x = 00000000 00000000 00000000 00001010
x |= (1 << 4); // x = 00000000 00000000 00000000 00011010
x |= (1 << 8); // x = 00000000 00000000 00000001 00011010
// Print the set elements
cout << "Set elements : ";
for (int i = 0; i < 32; ++i) {
if (x & (1 << i))
cout << i << ' ';
}
cout << '\n';
// Size of the set
cout << "Size = " << __builtin_popcount(x) << '\n';
return 0;
}