Knowee
Questions
Features
Study Tools

Given a non-negative integer N, the task is to check if N is a power of 2. Print 'YES' if N is the power of 2; else, print 'NO'.Example 1Input: N = 8Output: YESExplanation: 8 is equal to 2 raised to 3 (23 = 8).Example 2Input: N = 98Output: NOExplanation: 98 cannot be obtained by any power of 2.Note: This question was asked in Adobe coding test.Input format :The input consists of an integer N, representing the non-negative integer.

Question

Given a non-negative integer N, the task is to check if N is a power of 2. Print 'YES' if N is the power of 2; else, print 'NO'.Example 1Input: N = 8Output: YESExplanation: 8 is equal to 2 raised to 3 (23 = 8).Example 2Input: N = 98Output: NOExplanation: 98 cannot be obtained by any power of 2.Note: This question was asked in Adobe coding test.Input format :The input consists of an integer N, representing the non-negative integer.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, you can use a simple mathematical trick. Here are the steps:

  1. First, check if the number N is zero. If it is, print 'NO' because zero is not a power of 2.

  2. If N is not zero, perform a bitwise AND operation between N and (N-1). In Python, you can do this with the '&' operator. This operation will return zero if N is a power of 2.

  3. If the result of the bitwise AND operation is zero, print 'YES'. Otherwise, print 'NO'.

Here is a Python code snippet that implements these steps:

def is_power_of_two(N):
    if N == 0:
        return 'NO'
    else:
        return 'YES' if (N & (N-1)) == 0 else 'NO'

You can use this function to check if a number is a power of 2. For example, is_power_of_two(8) will return 'YES', and is_power_of_two(98) will return 'NO'.

This problem has been solved

Similar Questions

Find whether the given number is power of 2 or not.Input Format:Accept an Integer as inputOutput Format:Print the output as "YES" or "NO"Constraints:1<=INPUT<=10^15Sample Input 1:128Sample Output 1:YESSample Input 2:36Sample Output 2:NO

Given an integer n, return true if it is a power of two. Otherwise, return false.An integer n is a power of two, if there exists an integer x such that n == 2x.

Given base and exponent , find the power without using inbuilt functionInput FormatGiven two integers base and exponentOutput FormatPrint the powerConstraints1<= n <= 1000000NOTE : The calculated power value might exceed integer range.Sample Input 1:10 3Sample Output 1:1000Sample Input 2:3 4Sample Output 2:81

What output will the following code produce?n = 10while n != 1:    print (n,)    if n % 2 == 0: # n is even        n = n // 2    else: # n is odd        n = n * 3 + 1Question 17Select one:a.10 5 16 8 4 2b.None an error will be displayedc.8 4 2d.9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2

Given an integer n, return true if it is a power of three. Otherwise, return false.An integer n is a power of three, if there exists an integer x such that n == 3x.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.