By AADITYA RAJ

This is a C++ code to find whether a given number is a power of two or not. It is of a faster time complexity as compared to the one using the modulo operator and logarithmic operator.

Here in this code, I have used Bitwise Manipulation to find whether a given number is a power of 2 or not. This works on the principle that every power of 2 has only one bit set to 1 and the rest all to 0. Hence we use this to our advantage in a way that if n is the power of two then its only highest bit is set to one but for n-1 we have every bit less than the bit which is set in the case of n is set to 0. Thus by performing AND operation on n and n-1 gives us 0.

Submitted by AADITYA RAJ (AadiBaby)

Download packets of source code on Coders Packet