-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetOperationsImplementation.cpp
73 lines (62 loc) · 1.8 KB
/
SetOperationsImplementation.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Title : Implementation of Set operations via Bit operations
* Author : Tridib Samanta
**/
/*
Operation set syntax bit syntax
intersection a ∩ b a & b
union a U b a | b
complement ā ~a
difference a - b a & (~b)
*/
#include<bits/stdc++.h>
using namespace std;
int main() {
int a = (1 << 1) | (1 << 3) | (1 << 4) | (1 << 8); // a = {1, 3, 4, 8}
cout << "a : ";
for (int i = 0; i < 32; ++i) {
if (a & (1 << i))
cout << i << ' ';
}
cout << '\n';
int b = (1 << 3) | (1 << 6) | (1 << 8) | (1 << 9); // b = {3, 6, 8, 9}
cout << "b : ";
for (int i = 0; i < 32; ++i) {
if (b & (1 << i))
cout << i << ' ';
}
cout << '\n';
// x = a ∩ b -> a & b
int x = a & b; // x = {3,8}
cout << "a intersection b : ";
for (int i = 0; i < 32; ++i) {
if (x & (1 << i))
cout << i << ' ';
}
cout << '\n';
// x = a U b -> a | b
x = a | b; // x = {1, 3, 4, 6, 8, 9}
cout << "a union b : ";
for (int i = 0; i < 32; ++i) {
if (x & (1 << i))
cout << i << ' ';
}
cout << '\n';
// x = ā -> ~a
x = ~a; // x represents 28 elements, those which are not present in set 'a'
cout << "complement of a : ";
for (int i = 0; i < 32; ++i) {
if (x & (1 << i))
cout << i << ' ';
}
cout << '\n';
// x = a - b -> a & (~b)
x = a & (~b); // x = {1, 4}
cout << "difference of set B from set A : ";
for (int i = 0; i < 32; ++i) {
if (x & (1 << i))
cout << i << ' ';
}
cout << '\n';
return 0;
}