1) Simple tips to verify that certain number try an electrical energy of dos ? Thought several Letter and you ought to get a hold of in the event that N try an energy out of 2. Effortless option to this problem is to frequent split N from the 2 in the event the Letter is additionally. If we end up with a 1 upcoming N was fuel of dos, or even maybe not. Discover another type of case also. If Letter = 0 it is not an electrical energy from 2. Why don’t we password they.
A lot more than means tend to go back correct if x is actually an electricity off 2, or even incorrect. Date complexity of the significantly more than code try O(logN).
The same problem can be solved using bit manipulation. Consider a number x that we need to check for being a power for 2. Now think about the binary representation of (x-1). (x-1) will have all the bits same as x, except for the rightmost 1 in x and all the bits to the right of the rightmost 1. Let, x = 4 = (100)2 x – 1 = 3 = (011)2 Let, x = 6 = (110)2 x – 1 = 5 = (101)2