Solution Review: Power of 2
Explore the use of Brian Kernighan’s algorithm to efficiently verify if a number is a power of 2 by leveraging the bitwise AND operator. Understand the one-line solution, visualize binary operations, and analyze time and space complexity for optimized problem solving.
We'll cover the following...
We'll cover the following...
Solution review: Brian Kernighan’s algorithm
This is considered faster than the previous naive approach.
In this approach, we count the set bits. If a number is the power of 2, we know that only one set bit is present in its Binary representation.
In binary, we go from right to left with powers of 2.
For example:
, ...