Challenge 3: Power of 2
In this lesson, we will try to check if the given number is a power of 2. We solve this by writing an efficient algorithm that takes an optimal amount of time.
We'll cover the following...
Introduction
Let’s do another challenging question to check your understanding of Bitwise operators.
For example:
Input: 4
Output: True (As 4 is 2^2)
Input: 12
Output: False
Problem statement
Write a program to check if a given number is a power of 2 or not.
Let’s consider a number and find how the AND operator does this.
Input = 64
Output: True
Explanation: We solve by making use of the &
operator in computers. There are many ways to solve this, of which two approaches are ...
Access this course and 1400+ top-rated courses and projects.