This is for making computation faster in C++
This is for making computation faster in C++
Well i think that when ever needed we can make multiplications faster and divisions also by suing shifting instead of using the mul. So let us consider
for the 3*k+1 problem in computer science.
It says
1)Ask user for a number K
2)if K is even K<-K/2
else
K<-3*K+1
4)Check if K is equal to 1 Else go to step 2 again
Here the 3*K+1 and K/2 can be performed in the following manner:
K+=(K<<1)+1;//This is 3*K+1
and
K=K>>1;//This is K/2
11 Nov 05, 12:59AM
btw Most compilers will automatically convert k/2 into k>>1. So it often better to leave it in the more readable form. 8)
Login