Hi all, I am trying to raise an integer to the closest power of 2 (if already not one). Now in order to do this I first check for whether the number (say n) is power of 2. If it is , then I return. Now for doing this check I loop over integers :
example
I was wondering whether this is the most efficient method(which I am sure isn't especially when one considers the for loop). I know that probaly bitwise operations (XOR, AND etc.) could be used for the same and would be more efficient in a way. Could someone help me improve the effieciency of thisCode:int raise(int n) { for(int i=0;i<=n;i++) { temp=2**i; if(n == temp) return n; if(temp > n) { n=temp; return n; } } }