Challenge 3: Power of 2
Explore how to check if a number is a power of 2 by leveraging the bitwise AND operator. This lesson guides you through both a brute-force approach and hints at a more efficient method, deepening your understanding of bit manipulation and its optimization in coding.
We'll cover the following...
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 simple, and one of them ...