-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwiseExm.cpp.html
98 lines (77 loc) · 7.1 KB
/
bitwiseExm.cpp.html
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!-- HTML generated using hilite.me --><div style="background: #272822; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">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</span></pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #75715e">/*EXPLANATION CREDITS AREEB JAMAL*/</span>
<span style="color: #75715e">#include <iostream></span>
<span style="color: #66d9ef">using</span> <span style="color: #66d9ef">namespace</span> <span style="color: #f8f8f2">std;</span>
<span style="color: #66d9ef">int</span> <span style="color: #a6e22e">main</span><span style="color: #f8f8f2">(){</span>
<span style="color: #66d9ef">char</span> <span style="color: #f8f8f2">setA[</span><span style="color: #ae81ff">20</span><span style="color: #f8f8f2">]</span><span style="color: #f92672">=</span><span style="color: #f8f8f2">{</span><span style="color: #e6db74">'a'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'b'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'c'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'d'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'e'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'f'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'g'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'h'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'i'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'j'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'k'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'l'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'m'</span><span style="color: #f8f8f2">};</span>
<span style="color: #66d9ef">char</span> <span style="color: #f8f8f2">setB[</span><span style="color: #ae81ff">20</span><span style="color: #f8f8f2">]</span><span style="color: #f92672">=</span><span style="color: #f8f8f2">{</span><span style="color: #e6db74">'a'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'c'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'d'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'e'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'j'</span><span style="color: #f8f8f2">};</span>
<span style="color: #66d9ef">char</span> <span style="color: #f8f8f2">setC[</span><span style="color: #ae81ff">20</span><span style="color: #f8f8f2">]</span><span style="color: #f92672">=</span><span style="color: #f8f8f2">{</span><span style="color: #e6db74">'g'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'h'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'l'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'m'</span><span style="color: #f8f8f2">,</span><span style="color: #e6db74">'d'</span><span style="color: #f8f8f2">};</span>
<span style="color: #66d9ef">int</span> <span style="color: #f8f8f2">sB;</span>
<span style="color: #75715e">/*int set function can be made that can be called at </span>
<span style="color: #75715e">each sub SET to set the binary value of the SET which can later </span>
<span style="color: #75715e">be used to do bitwise operations*/</span>
<span style="color: #66d9ef">for</span><span style="color: #f8f8f2">(</span><span style="color: #66d9ef">int</span> <span style="color: #f8f8f2">i</span><span style="color: #f92672">=</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;i</span><span style="color: #f92672"><</span><span style="color: #ae81ff">13</span><span style="color: #f8f8f2">;i</span><span style="color: #f92672">++</span><span style="color: #f8f8f2">){</span>
<span style="color: #66d9ef">for</span><span style="color: #f8f8f2">(</span><span style="color: #66d9ef">int</span> <span style="color: #f8f8f2">j</span><span style="color: #f92672">=</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;j</span><span style="color: #f92672"><</span><span style="color: #ae81ff">5</span><span style="color: #f8f8f2">;j</span><span style="color: #f92672">++</span><span style="color: #f8f8f2">){</span>
<span style="color: #66d9ef">if</span><span style="color: #f8f8f2">(setA[i]</span><span style="color: #f92672">==</span><span style="color: #f8f8f2">setB[j]){</span>
<span style="color: #f8f8f2">sB</span> <span style="color: #f92672">|=</span><span style="color: #f8f8f2">(</span><span style="color: #ae81ff">1</span><span style="color: #f92672"><<</span><span style="color: #f8f8f2">i);</span> <span style="color: #75715e">//this is where the bits are been set</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">cout</span><span style="color: #f92672"><<</span><span style="color: #f8f8f2">sB</span><span style="color: #f92672"><<</span><span style="color: #f8f8f2">endl;</span> <span style="color: #75715e">//this the decimal value of the setB elemnts.In this case 541</span>
<span style="color: #75715e">/*This is the simple print call that prints the values of the set or</span>
<span style="color: #75715e">the content of the set .Here setB elements are printed */</span>
<span style="color: #66d9ef">for</span><span style="color: #f8f8f2">(</span><span style="color: #66d9ef">int</span> <span style="color: #f8f8f2">i</span><span style="color: #f92672">=</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;i</span><span style="color: #f92672"><</span><span style="color: #ae81ff">12</span><span style="color: #f8f8f2">;i</span><span style="color: #f92672">++</span><span style="color: #f8f8f2">){</span>
<span style="color: #66d9ef">if</span><span style="color: #f8f8f2">(sB</span> <span style="color: #f92672">&</span> <span style="color: #f8f8f2">(</span><span style="color: #ae81ff">1</span><span style="color: #f92672"><<</span><span style="color: #f8f8f2">i))</span> <span style="color: #75715e">//Each bit of sB in binary form is "AND" with 0 then 10 then 100 and so on until it checks all the bits of sB</span>
<span style="color: #75715e">//which ever found true its then printed using universal set</span>
<span style="color: #f8f8f2">cout</span><span style="color: #f92672"><<</span><span style="color: #f8f8f2">setA[i];</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #66d9ef">return</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">}</span>
</pre></td></tr></table></div>