-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitMap.cpp
More file actions
121 lines (112 loc) · 1.8 KB
/
BitMap.cpp
File metadata and controls
121 lines (112 loc) · 1.8 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
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
bool isOdd(ll x)
{
return (x & 1)?true:false;
}
vector<string> split(const string& str, char delim) {
vector<string> strings;
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != string::npos) {
end = str.find(delim, start);
strings.push_back(str.substr(start, end - start));
}
return strings;
}
//get ith bit
ll getBit(ll x,ll i)
{
return (x & (1<<i))?1:0;
}
void setBit(ll &x,ll i)
{
x= (x | (1<<i)) ;
return;
}
void clearBit(ll &x, ll i)
{
ll mask = ~(1<<i);
x = (x & mask);
return ;
}
void updateBit(ll &n,ll i, ll v)
{
clearBit(n,i);
ll mask = v<<i;
n = (n | mask);
return;
}
ll clearLastIBits(ll n,ll i)
{ // i equals no of bits cleared
ll allOnes = ~0;
return (n & (allOnes<<i));
}
ll clearItoJBits(ll n,ll i,ll j)
{ //i<j and indexinf from 0 starting from right
ll allOnes = ~0;
ll OnesAfterJ = allOnes<<(j+1); //111100000
ll OnesBeforeI = (1<<i)-1; //000000011 where i is 2;
ll mask = (OnesBeforeI | OnesAfterJ );//111100011
return n & mask;
}
int countSetBits(ll n)
{
int ans=0;
while(n>0)
{
ans += (n & 1);
n=n>>1;
}
return ans;
}
int countSetBitsFast(ll n)
{
int ans=0;
while(n>0)
{
n = n & (n-1);//will remove the LSB
ans++;
}
return ans;
//__builtin_popcount() can also be used;
}
ll decimalToBin(ll n)
{
ll ans=0,p=1;
while(n>0)
{
ll currBit = n & 1;
n=n>>1;
ans+=p*currBit;
p*=10;
}
return ans;
}
ll fast_expo(int a,int n)
{
ll ans=1;
while(n>0)
{
int last_bit = n & 1;
if(last_bit)
{
ans*=a;
}
n = n>>1;
a = a*a;
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("opps.txt","w",stdout);
#endif
int n,a;
cin>>a>>n;
cout<<fast_expo(a,n)<<endl;
return 0;
}