-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTricks.cpp
More file actions
146 lines (116 loc) · 2.64 KB
/
BinaryTricks.cpp
File metadata and controls
146 lines (116 loc) · 2.64 KB
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
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("-ffloat-store")
#pragma GCC optimize ("Ofast")
static auto _ = [] () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}();
#define frw(i, len) for(ll i = 0 ; i < len ; i++)
#define frb(i, len) for(ll i = len ; i >= 0 ; i--)
#define YES cout<<"Yes \n";
#define NOO cout<<"No \n";
#define nl cout<<"\n";
#define MAX_SIZE 10000
#define nptr nullptr
typedef stringstream strgm;
typedef long long int ll;
typedef const int c_int;
typedef unsigned unsg;
typedef double dbl;
typedef vector<vector<string>> vvstr;
typedef vector<vector<bool>> vvbl;
typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<string> vstr;
typedef vector<int> vint;
typedef vector<bool> vbl;
typedef vector<ll> vll;
typedef stack<string> sstr;
typedef stack<bool> sbl;
typedef stack<int> sint;
typedef stack<ll> sll;
typedef queue<string> qstr;
typedef queue<bool> qbl;
typedef queue<int> qint;
typedef queue<ll> qll;
c_int MOD=1e9 + 7;
c_int Mx_row=100;
c_int Mx_col=100;
int size_stack=0;
int InvrsnCnt=0;
int size_arr=0;
int size_ll=0;
int top=-1;
void PrntBnry(int n){
for(int i = 10;i >= 0;i--){
cout<<((n>>i)&1);
}
nl;
}
int SetBit(int a,int i){
return ((1<<i) | a);
}
int UnSetBit(int a,int i){
return ((~(1<<i))&a);
}
int GetBit(int a,int i){
return ((1<<i) & a);
}
int Toggle(int a,int i){
return ((1<<i) ^ a);
}
int ClearMSB(int a,int i){
// PrntBnry((~((1<<(i+1))-1))); // 4
return ((~((1<<(i+1))-1)) & a);
}
int main(){
int a = 9;
// PrntBnry(a);
// Set Bit
int b,i = 2;
b = SetBit(a,i); // should be 13
// cout<<b;nl;
// PrntBnry(b);
// Get Bit
i = 0;
// cout<<((GetBit(a,i) == 1) ? "Bit is Set" : "Bit is not Set");nl;
// UnSetting a Bit
i=3;
int d = UnSetBit(a,i);
// PrntBnry(d);
// Toggle a bit
i = 1;
int e = Toggle(a,i);
// cout<<e;nl;
// count no of bits
int f = 8;
// cout<<__builtin_popcount(f);nl;
ll g = 1LL<<35;
// cout<<__builtin_popcountll(g);nl;
char c1 = 'A';
// cout<<(char)(c1 | ' ');nl;
char c2 = 'a';
// cout<<(char)(c2 & '_');nl;
int h = 59;
// PrntBnry(h); // 1
// PrntBnry(h);
i = 4;
int one = ClearMSB(h,i);
// PrntBnry(one); // 3
cout<<one;nl; // 4
int j = 16;
if(!(j&(j-1))){
cout<<"Power of two";nl;
}else{
cout<<"Not a Power of two";nl;
}
return 0;
}
// int main(){
// PrntBnry(1<<5);
// }