{"id":8276,"date":"2021-06-07T18:07:44","date_gmt":"2021-06-07T12:37:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=8276"},"modified":"2021-11-22T18:38:39","modified_gmt":"2021-11-22T13:08:39","slug":"python-program-to-find-whether-a-number-is-a-power-of-two","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-whether-a-number-is-a-power-of-two\/","title":{"rendered":"Python Program to Find Whether a Number is a Power of Two"},"content":{"rendered":"
In this tutorial, the power of 2 or not for python programming can be checked. The user gives a positive integer N and we have to check if it is equivalent to 2^x or not, where x can be zero or a number positive.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to check whether the given number is a power of 2 or not some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Continue dividing the number into two, i.e. do n = n\/2 as long as n is 1 If n percent 2 is non-zero and n is not 1 in all iteration, then n is not 2. It is a power of 2, if n becomes 1.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If the number is 2 power, the binary representation\u00a0of only 1 bit is set. Therefore, if a number is 2, then<\/p>\n numb&(numb-1) == 0<\/b><\/p><\/blockquote>\n Only if N is zero is the exception to the foregoing rule. As a power of two, it will give zero which in fact is not. Therefore, we can explicitly see this in the check.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n A simple method is to take each bit into consideration in a number (set or unset) and hold a counter to track the set bits if the total number of set bits is 1 then the number is power of 2 else it is not the power of 2<\/p>\n Approach:<\/strong><\/p>\n We use\u00a0n&1<\/strong> to check whether it is set bit or not.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In this tutorial, the power of 2 or not for python programming can be checked. The user gives a positive integer N and we have to check if it is equivalent to 2^x or not, where x can be zero or a number positive. Examples: Example1: Input: given number =2048 Output: The given numb 2048 …<\/p>\ngiven number =2048<\/pre>\n
The given numb 2048 is power of 2<\/pre>\n
given number =256<\/pre>\n
The given numb 256 is power of 2<\/pre>\n
given number =678<\/pre>\n
The given numb 678 is not power of 2<\/pre>\n
Program to Find Whether a Number is a Power of Two in Python<\/h2>\n
\n
Method #1:Using log function<\/h3>\n
\n
# importing log from math\r\nfrom math import log\r\n# given number\r\nnumb = 2048\r\n# given base (here it is 2 as we have to check whether it is power of 2 or not)\r\nbas = 2\r\n# calculating log2 of the given numb using log function\r\nlogvalue = log(numb, bas)\r\n# if the value is integer then it is power of 2\r\nif (int(logvalue) == logvalue):\r\n print(\"The given numb\", numb, \"is power of 2\")\r\n# else it is not power of 2\r\nelse:\r\n print(\"The given numb\", numb, \"is not power of 2\")\r\n<\/pre>\n
The given numb 2048 is power of 2<\/pre>\n
Method #2:Using while loop<\/h3>\n
# function which returns true if the given number is power of 2 else it reeturns false\r\n\r\n\r\ndef checktwoPower(numb):\r\n # checking if the number is 0\r\n if (numb == 0):\r\n # if the number is 0 then it is not power of 2 hence return false\r\n return False\r\n # using while loop to iterate till the number not equal to 1\r\n while (numb != 1):\r\n # if the bit is set bit then it is not power of 2 so return False\r\n if (numb % 2 != 0):\r\n return False\r\n # dividing n\r\n numb = numb \/\/ 2\r\n return True\r\n\r\n\r\n# given number\r\nnumb = 2048\r\n# passing the given number to checktwoPower function\r\n# if the value is integer then it is power of 2\r\nif (checktwoPower(numb)):\r\n print(\"The given numb\", numb, \"is power of 2\")\r\n# else it is not power of 2\r\nelse:\r\n print(\"The given numb\", numb, \"is not power of 2\")\r\n<\/pre>\n
The given numb 2048 is power of 2<\/pre>\n
Method #3:Using Bitwise Operators<\/h3>\n
\nIf we remove 1 from a power number 2, then all the bits are set and the set bit is unset after the set bits (just one bits are set according to point-1)<\/p>\n# function which returns true if the given number is power of 2 else it reeturns false\r\n\r\n\r\ndef checktwoPower(numb):\r\n return numb and (not(numb & (numb-1)))\r\n\r\n\r\n# given number\r\nnumb = 2048\r\n# passing the given number to checktwoPower function\r\n# if the value is integer then it is power of 2\r\nif (checktwoPower(numb)):\r\n print(\"The given numb\", numb, \"is power of 2\")\r\n# else it is not power of 2\r\nelse:\r\n print(\"The given numb\", numb, \"is not power of 2\")\r\n<\/pre>\n
The given numb 2048 is power of 2<\/pre>\n
Method #4:By Calculating total number of set bits<\/h3>\n
\n
# function which returns true if the given number is power of 2 else it reeturns false\r\n\r\n\r\ndef checktwoPower(numb):\r\n # checking if the given number is greater than 1\r\n if numb > 1:\r\n # Set the variable say setbitcount to 0 to count the total number of set bits.\r\n setbitcount = 0\r\n # looping till number greater than 0 using while loop\r\n while(numb > 0):\r\n # We will get the last check bit whether it is set bit or not using & operator\r\n # checking if the check bit is 1 or not\r\n # if the check bit is 1 then increment the setbitcount\r\n if(numb & 1):\r\n setbitcount = setbitcount+1\r\n # divide the number by 2\r\n numb = numb\/\/2\r\n # checking if the total number of setbits are 1 or not\r\n return (setbitcount == 1)\r\n\r\n\r\n# given number\r\nnumb = 2048\r\n# passing the given number to checktwoPower function\r\n# if the value is integer then it is power of 2\r\nif (checktwoPower(numb)):\r\n print(\"The given numb\", numb, \"is power of 2\")\r\n# else it is not power of 2\r\nelse:\r\n print(\"The given numb\", numb, \"is not power of 2\")\r\n<\/pre>\n
The given numb 2048 is power of 2<\/pre>\n
\n