{"id":20526,"date":"2021-09-15T19:50:38","date_gmt":"2021-09-15T14:20:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=20526"},"modified":"2021-11-22T18:36:18","modified_gmt":"2021-11-22T13:06:18","slug":"python-program-to-check-if-a-number-has-bits-in-alternate-pattern","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-number-has-bits-in-alternate-pattern\/","title":{"rendered":"Python Program to Check if a Number has Bits in Alternate Pattern"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find XOR of Two Numbers Without Using XOR operator<\/a><\/p>\n Given a number, the task is to check if the given Number has the bits in an alternate pattern<\/p>\n For example, the number 42 has an alternate pattern, which is 101010 (after 1 we are getting 0 and next 1 so on). 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 Below are the ways to check if a given number has bits in an alternate pattern in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dive into numerous Python Programming Language Examples<\/a> for practice and get the best out of the tutorial and learn python one step at a time.<\/p>\n In the previous article, we have discussed Python Program to Find XOR of Two Numbers Without Using XOR operator Given a number, the task is to check if the given Number has the bits in an alternate pattern For example, the number 42 has an alternate pattern, which is 101010 (after 1 we are getting …<\/p>\n
\nIf it has an alternate bit pattern, print “Yes,” else “No.”<\/p>\nGiven Number= 42<\/pre>\n
Yes, the given number{ 42 } has an alternate bit pattern<\/pre>\n
Given Number= 14<\/pre>\n
No, the given number{ 14 } doesn't have an alternate bit pattern<\/pre>\n
Program to Check if a Number has Bits in Alternate Pattern in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Create a function to say chek_alterntebit which takes the given number as an\r\n# argument and returns true if it has an alternate bit pattern else returns False.\r\n\r\n\r\ndef chek_alterntebit(gvn_numbr):\r\n # Apply given number modulus 2 to get the last bit and store it in a variable.\r\n lst_bit = gvn_numbr % 2\r\n # Divide the given number by 2 and store it in the same variable gvn_numbr.\r\n gvn_numbr = gvn_numbr \/\/ 2\r\n # Loop until the given number is greater than 0 using a while loop.\r\n while (gvn_numbr > 0):\r\n # Inside the loop, calculate the current bit by applying the given number modulus 2\r\n # and store it in another variable.\r\n\r\n prsent_bit = gvn_numbr % 2\r\n # Check if the current bit is equal to the last bit using the if conditional\r\n # statement.\r\n if (prsent_bit == lst_bit):\r\n # If the statement is true, then return false.\r\n return False\r\n # Assign the current bit to the last bit.\r\n lst_bit = prsent_bit\r\n # Divide the given number by 2 and store it in the same variable gvn_numbr.\r\n gvn_numbr = gvn_numbr \/\/ 2\r\n # Return True. (out of while loop)\r\n return True\r\n\r\n\r\n# Give the number as static input and store it in a variable.\r\ngvn_numbr = 42\r\n# Pass the given number as an argument to the chek_alterntebit function.\r\n# Check if the chek_alterntebit(gvn_numbr) using the if conditional statement.\r\nif(chek_alterntebit(gvn_numbr)):\r\n # If the statement is true, print \"Yes, the given number has an alternate bit pattern\".\r\n print(\"Yes, the given number{\", gvn_numbr,\r\n \"} has an alternate bit pattern\")\r\nelse:\r\n # Else print \"No, the given number doesn't have an alternate bit pattern\".\r\n print(\"No, the given number{\", gvn_numbr,\r\n \"} doesn't have an alternate bit pattern\")\r\n<\/pre>\n
Yes, the given number{ 42 } has an alternate bit pattern<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Create a function to say chek_alterntebit which takes the given number as an\r\n# argument and returns true if it has an alternate bit pattern else returns False.\r\n\r\n\r\ndef chek_alterntebit(gvn_numbr):\r\n # Apply given number modulus 2 to get the last bit and store it in a variable.\r\n lst_bit = gvn_numbr % 2\r\n # Divide the given number by 2 and store it in the same variable gvn_numbr.\r\n gvn_numbr = gvn_numbr \/\/ 2\r\n # Loop until the given number is greater than 0 using a while loop.\r\n while (gvn_numbr > 0):\r\n # Inside the loop, calculate the current bit by applying the given number modulus 2\r\n # and store it in another variable.\r\n\r\n prsent_bit = gvn_numbr % 2\r\n # Check if the current bit is equal to the last bit using the if conditional\r\n # statement.\r\n if (prsent_bit == lst_bit):\r\n # If the statement is true, then return false.\r\n return False\r\n # Assign the current bit to the last bit.\r\n lst_bit = prsent_bit\r\n # Divide the given number by 2 and store it in the same variable gvn_numbr.\r\n gvn_numbr = gvn_numbr \/\/ 2\r\n # Return True. (out of while loop)\r\n return True\r\n\r\n\r\n# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numbr = int(input('Enter some random number = '))\r\n# Pass the given number as an argument to the chek_alterntebit function.\r\n# Check if the chek_alterntebit(gvn_numbr) using the if conditional statement.\r\nif(chek_alterntebit(gvn_numbr)):\r\n # If the statement is true, print \"Yes, the given number has an alternate bit pattern\".\r\n print(\"Yes, the given number{\", gvn_numbr,\r\n \"} has an alternate bit pattern\")\r\nelse:\r\n # Else print \"No, the given number doesn't have an alternate bit pattern\".\r\n print(\"No, the given number{\", gvn_numbr,\r\n \"} doesn't have an alternate bit pattern\")\r\n<\/pre>\n
Enter some random number = 14\r\nNo, the given number{ 14 } doesn't have an alternate bit pattern<\/pre>\n
\n