menu
techminis

A naukri.com initiative

google-web-stories
source image

Prodevelopertutorial

1M

read

203

img
dot

Power of 2

  • Given a number, you need to check if the number is a power of 2, return true or false accordingly.
  • We can solve this by 2 methods: 1. Iterative method 2. Bitwise Operation
  • In the iterative method, we divide the number by 2 repeatedly and check the modulo of the number. If the mod value is equal to 0, then we return true, if the mod value is not ‘0’, then we return false.
  • In bitwise operation, we perform a bitwise “&” of the current number and its previous number. If the value is equal to “0” then we return true, else false.

Read Full Article

like

12 Likes

For uninterrupted reading, download the app