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:

  • Resillience
  • Multi-Head Attention
  • Preference Alignment 101
  • Challenges in Code Generation
  • PREDICTING AND OPTIMIZING LLVM COMPILER PASS ORDER