bitmagic.
cpp
1 //********** BITWISE OPERATOR IN CPP ******
2 /*
3 #include <iostream>
4 using namespace std;
5 int main()
6 {
7 int n, m;
8 cin >> n >> m;
9 cout << " OR =" << (m | n) << endl;
10 cout << " AND =" << (m & n) << endl;
11 cout << " XOR =" << (m ^ n) << endl;
12 cout << (n << 1) << endl;
13 cout << (n << 2) << endl;
14 cout << (n << 3) << endl;
15 cout << (n >> 1) << endl;
16 cout << (n >> 2) << endl; // left
17 }
18 */
19
20 //**BITWISE NOT **
21 /*
22 #include <iostream>
23 using namespace std;
24
25 int main()
26 {
27 unsigned int x=1,y=5;
28 cout<<(~x);
29
30 cout<<endl<<~y<<endl;
31 signed int i=1,j=5;
32 cout<<(~i);//(2^32-1)-1=-2//Don't use right left shift for negative no.s
33
34 cout<<endl<<~j<<endl;
35
36 }
37 */
38
39 //
40 /*
41 //QUESTION TO CHICK IF Kth BIT IS SET(1)
42 #include <iostream>
43 using namespace std;
44
45 int main()
46 {int n,k;
47 cin>>n>>k;
48
49 if(((n>>k-1)&1)==1){
50 cout<<"yes"<<endl;}
51 else cout<<"no";
52 }
53 */
54
55 /*
56 // QUESTION TO count BIT IS SET(1) in given no.
57 #include <iostream>
58 using namespace std;
59 void count1(int n)
60 { // O(bits)
61 int k = 0;
62 while (n != 0)
63 {
64 if ((n & 1) == 1)
65 {
66 k++;
67 }
68 n = n >> 1;
69 }
70 cout << k;
71 }
72 void countbits(int n)
73 { // theta(SET BIT COUNT)//BRIAN'S KERNIGAM'S ALGORITHAM
74 int k = 0;
75 while (n > 0)
76 {
77 n = (n & (n - 1));
78 k++;
79 }
80 cout << k;
81 }
82
83 int table[256];
84 void initializer(int n){
85 table[0]=0;
86 for(int i=1;i<256;i++){
87 table[i]=(i&1)+table[i/2];
88 }
89 }
90 void count(int n){initializer( n); //tc theta(1)
91 int res=table[(n&0xff)];//Oxff means 8times 1.
92 n=n>>8;
93 res= res+table[(n&0xff)];
94 n=n>>8;
95 res= res+table[(n&0xff)];
96 n=n>>8;
97 res= res+table[(n&0xff)]; //4*8=32bits
98
99 cout<<res;
100 }
101 int main()
102 {
103 int n;
104 cin >> n;
105 count(n);
106 }
107 */
108
109 /*
110 //to check whether give no. is power of two or not
111 #include <iostream>
112 using namespace std;
113 bool checkpowerof2(int n){
114 return((n!=0)&&((n&(n-1))==0)); // means if n==0 then return false and if n&(n-1)!=0
return false //here the condition written in return will return true.
115 }
116 int main(){
117 int n;cin>>n;
118 cout<< checkpowerof2(n);
119 }
120 */
121
122 /*
123 //print the only odd(only one odd is present) occuring no. in given array
124 #include<iostream>
125 using namespace std;
126 int findodd(int arr[],int n){
127 int res=0;
128 for(int i=0;i<n;i++)
129 {
130 res=res^arr[i];
131 }return res;}
132 int main(){
133 int a[]={5,6,4,6,4,5,5};
134 cout<<findodd(a,7);
135 }
136
137 */
138
139 /*
140 //Give an array having values from 1,2to n+1 b\w it one value is mnissing print find than
value
141 //for solving it we will take xor of 1 to n+1 with array values
142 #include<iostream>
143 using namespace std;
144 int findmissing(int arr[],int n){
145 int res=0;
146 for(int i=1;i<=n+1;i++){
147 res=res^i;
148 }
149 for(int i=0;i<n;i++){
150 res=res^arr[i];
151 }return res;
152
153 }
154 int main(){
155 int a[6]={1,2,7,3,5,6};
156 cout<<findmissing(a,6);
157 }
158 */
159
160 /*
161 // print the only two odd(only two odd ocuuring is present) occuring no. in given array
162 // in it we break the set in two set in which each set will have only one odd ocuuring
value(we will do it using rightmost setbit of xor (using trick as 1
163 // will only come in xor if operands have different values one had 0 and other 1 at that
position then with this position we will make two sets and will take xor of both to get
odd occuring from each) )
164 #include <iostream>
165 using namespace std;
166 void oddocurring(int arr[], int n)
167 {
168 int res1 = 0, XOR = 0;
169 int res2 = 0;
170 for (int i = 0; i < n; i++)
171 {
172 XOR = XOR^arr[i];
173 }
174 int sn = XOR & ( ~(XOR-1)); // TO FIND RIGHT MOST SETBIT(1) //~(XOR-1)CHANGES ALL
BITS OF XOR LEFT TO THE RIGHTMOST SET BIT.Ex-1010100 changes to 0101100. then sn =100
175 for (int j = 0; j < n; j++)
176 {
177 if ((arr[j] & sn) != 0)
178 {
179 res1 = res1 ^ arr[j];
180 }
181 else
182 {
183
184 res2 = res2 ^ arr[j];
185 }
186 }
187 cout << res1 << endl<< res2;
188 }
189
190 int main()
191 {
192 int a[] = {5, 6, 6, 4, 6, 4, 5, 5};
193 oddocurring(a, 8);
194 }
195
196 */
197
198 /*
199 // printing power set using bitwise operator //000 null ,001 a,010,b,100,c,111,abc
200 #include <iostream>
201 #include <math.h>
202 #include<string>
203 using namespace std;
204 void printpowerset(string str) //tc theta(n*2^n)
205 {
206 int n = str.length();
207 int powersize = pow(2, n);
208 for (int counter = 0; counter < powersize; counter++)
209 {
210 for (int j = 0; j < n; j++)
211 {
212 if ((counter & (1 << j)) != 0)
213 {
214 cout << str[j];
215 }
216 }
217 cout << endl;
218 }
219 }
220 int main()
221 {
222 string a = "abc";
223 printpowerset(a);
224 }
225 */
226
227 /*
228 //find sq. of given no. by using bits manupulation(without multiplying or pow)
229
230 #include <iostream>
231 using namespace std;
232 int printsqofn(int n)
233 {
234 int res = 0, j = n;
235 for (int i = 0; j > 0; i++,j= (j >> 1))
236 {if(j&1==1){
237 res += (n << i);}
238 }
239 return res;
240 }
241 int main()
242 {
243 cout << printsqofn(3) << endl;
244
245 }
246 */