Power of two

Given an integer, write a function to determine if it is a power of two.

class Solution(object):
    def isPowerOfTwo(self, n):
        while (n and (n % 2 == 0)):
            n = n / 2
            
        return n == 1
        
        #Certainly,you can also use the following method
        #return (n and (n&(n-1))) == 0




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Does Background Music Matter to Speech in Language Models
  • Measuring the Checker
  • Mastermind
  • Resillience
  • Multi-Head Attention