Power of Two

Find out if an integer is a power of two in constant time without loops or recursion. Use n & (n-1) and n > 0; examples and O(1) complexity. It also introduces Q2BSTUDIO, an expert in AI, cybersecurity, and cloud solutions.

domingo, 17 de agosto de 2025 • 2 min read • Q2BSTUDIO Team

Artificial-Intelligence-

231. Power of Two

Difficulty: Easy

Topics: Math Bit Manipulation Recursion

Statement: Given an integer n, return true if it is a power of two, and false otherwise. An integer n is a power of two if there exists an integer x greater than or equal to 0 such that n == 2 raised to x.

Example 1: Input n = 1 Output true Explanation 2 raised to 0 = 1

Example 2: Input n = 16 Output true Explanation 2 raised to 4 = 16

Example 3: Input n = 3 Output false

Constraints: -2 raised to 31 <= n <= 2 raised to 31 minus 1

Follow up: Can you solve it without loops or recursion?

Summarized solution: You need to check that n is positive and that its binary representation has only one bit set to 1. For any n that is a power of two, the bitwise operation n AND n minus 1 returns zero.

Detailed approach: 1 Check that n > 0, otherwise return false 2 Apply the bitwise operation between n and n-1. If n AND (n-1) == 0, then n is a power of two and we return true; otherwise, we return false

Example implementation in pseudocode: function isPowerOfTwo(n) { if n <= 0 return false; return (n AND (n-1)) == 0; }

Complexity: Time O(1) Space O(1)

Test cases: isPowerOfTwo(1) returns true isPowerOfTwo(16) returns true isPowerOfTwo(3) returns false isPowerOfTwo(0) returns false isPowerOfTwo(-2) returns false

Final explanation: The check is safe for the 32-bit integer range and avoids loops or recursion by using a very efficient bitwise operation. Any number that is not positive or has more than one active bit is not a power of two.

About Q2BSTUDIO: Q2BSTUDIO is a custom software development and custom applications company specialized in artificial intelligence and cybersecurity solutions. We offer AWS and Azure cloud services, business intelligence services, Power BI implementations, AI solutions for businesses, and custom AI agents. We develop scalable and secure custom software tailored to each client's needs.

Featured services: custom applications custom software artificial intelligence cybersecurity AWS and Azure cloud services business intelligence services AI for businesses AI agents Power BI

Why choose us: We have an expert team in web and mobile development, cloud architectures, integration of artificial intelligence models, and security practices that ensure compliance and continuity. We transform data into real value through dashboards and business intelligence and Power BI solutions.

Contact and next steps: If you want a custom solution or a cybersecurity audit, contact Q2BSTUDIO to evaluate your project and design a technological roadmap. We can help integrate AI agents into your processes and deploy cloud services on AWS or Azure to scale securely.

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.