long long fast_exp(long long int base,long long int exp,int p) {
int res=1;
while(exp>0) {
if(exp%2==1)
{res=(res*base)%p;}
exp=exp>>1;
base=(base*base)%p;
}
return res;
}
It is a function of modular exponentiation. I want to ask about this while loop. When does this loop terminate? Because exp
is always greater than 0. I also don't understand this loop in that how it run and how it works line by line. I don't understand the approach of this loop.