-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bits manipulation (Important tactics).txt
173 lines (151 loc) · 3.94 KB
/
Bits manipulation (Important tactics).txt
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
Bits manipulation (Important tactics)
Table of content
1)bitwise shift
2)quickly check if a number is odd or even
3)Direct XOR of all numbers from 1 to n
4)isPowerOfTwo function by / and checking
5) by one liner expression bool
6) Count the number of ones in the binary of number.
7)print all possible subset of a set
---------------------------------------------------------
1) **bitwise shift**
#include <stdio.h>
int main()
{
int x = 19;
printf("x << 1 = %d\n", x << 1); //left shift
printf("x >> 1 = %d\n", x >> 1); //right shift
return 0;
}
----------------------------------------------
Output:
x << 1 = 38 x ko badhava mila
x >> 1 = 9 x ko kheench liya
------------------------------------------
2)
**even odd**
The & operator can be used to quickly check if a number is odd or even.
#include <stdio.h>
int main()
{
int x = 19;
(x & 1) ? printf("Odd") : printf("Even");
return 0;
}
-----------------------------
Output:
Odd
3)
*** Direct XOR of all numbers from 1 to n ***
int computeXOR(int n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
else
return 0;
}
--------------------------
Input: 6
Output: 7
----------------------------
4)
check if thde number is power of 2
bool isPowerOfTwo(int x)
{
if(x == 0)
return false;
else
{
while(x % 2 == 0) x /= 2;
return (x == 1);
}
}
-----------------------------------------
5)
bool isPowerOfTwo(int x)
{
// x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2 or not
return (x && !(x & (x - 1)));
}
example =
Let, x = 4 = (100)2
x - 1 = 3 = (011)2
x & (x-1) = 4 & 3 = (100)2 & (011)2 = (000)2
--------------------------------------------
6)
int count_one (int n)
{
while( n )
{
n = n&(n-1);
count++;
}
return count;
}
Example:
n = 23 = {10111}2 .
1. Initially, count = 0.
2. Now, n will change to n&(n-1). As n-1 = 22 = {10110}2 , then n&(n-1) will be {101112 & {10110}2, which will be {10110}2 which is equal to 22. Therefore n will change to 22 and count to 1.
3. As n-1 = 21 = {10101}2 , then n&(n-1) will be {10110}2 & {10101}2, which will be {10100}2 which is equal to 20. Therefore n will change to 20 and count to 2.
4. As n-1 = 19 = {10011}2 , then n&(n-1) will be {10100}2 & {10011}2, which will be {10000}2 which is equal to 16. Therefore n will change to 16 and count to 3.
5. As n-1 = 15 = {01111}2 , then n&(n-1) will be {10000}2 & {01111}2, which will be {00000}2 which is equal to 0. Therefore n will change to 0 and count to 4.
6. As n = 0, the the loop will terminate and gives the result as 4.
--------------------------------------------------------------------------------------------------------------
7)
C++ program to print all possible subset of a set
void allPossibleSubset(int arr[],int n)
{
int count = pow(2,n);
for (int i = 0; i < count; i++)
{
// The inner for loop will run n times , As the maximum number of elements a set can have is n
// This loop will generate a subset
for (int j = 0; j < n; j++)
{
// This if condition will check if jth bit in binary representation of i is set or not
// if the value of (i & (1 << j)) is greater than 0 , include arr[j] in the current subset
// otherwise exclude arr[j]
if ((i & (1 << j)) > 0)
cout << arr[j] << " ";
}
cout << "\n";
}
}
int main()
{
int n;
cin >> n;
int arr[n];
for(int i=0;i<n;i++)
{
cin >> arr[i];
}
allPossibleSubset(arr,n);
return 0;
}
**
example-
Enter size of the set
4
Enter Elements of the set
1 2 3 4
1
2
1 2
3
1 3
2 3
1 2 3
4
1 4
2 4
1 2 4
3 4
1 3 4
2 3 4
1 2 3 4
-----------------------------------------------------