{"id":20529,"date":"2021-09-15T19:50:41","date_gmt":"2021-09-15T14:20:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=20529"},"modified":"2021-11-22T18:36:18","modified_gmt":"2021-11-22T13:06:18","slug":"python-program-to-count-minimum-bits-to-flip-such-that-xor-of-a-and-b-equal-to-c","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-minimum-bits-to-flip-such-that-xor-of-a-and-b-equal-to-c\/","title":{"rendered":"Python Program to Count Minimum Bits to Flip such that XOR of A and B Equal to C"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check if a Number has Bits in Alternate Pattern<\/a><\/p>\n Given an N-bit binary sequence consisting of three binary sequences A, B, and C. Count the minimal number of bits required to flip A and B in such a way that the XOR of A and B equals C.<\/p>\n X\u00a0 \u00a0 \u00a0 \u00a0Y\u00a0 \u00a0 \u00a0 X XOR Y<\/strong><\/p>\n 0\u00a0 \u00a0 \u00a0 \u00a00\u00a0 \u00a0 \u00a0 \u00a0 0<\/p>\n 0\u00a0 \u00a0 \u00a0 \u00a01\u00a0 \u00a0 \u00a0 \u00a0 1<\/p>\n 1\u00a0 \u00a0 \u00a0 \u00a00\u00a0 \u00a0 \u00a0 \u00a0 1<\/p>\n 1\u00a0 \u00a0 \u00a0 \u00a01\u00a0 \u00a0 \u00a0 \u00a0 0<\/p>\n Cases:<\/strong><\/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 Below are the ways to Count the minimal number of bits required to flip A and B in such a way that the XOR of A and B equals C:<\/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 Enhance your coding skills with our list of Python Basic Programs<\/a> provided and become a pro in the general-purpose programming language Python in no time.<\/p>\n In the previous article, we have discussed Python Program to Check if a Number has Bits in Alternate Pattern Given an N-bit binary sequence consisting of three binary sequences A, B, and C. Count the minimal number of bits required to flip A and B in such a way that the XOR of A and …<\/p>\n\n
Given number = 5\r\nGiven first string= \"11011\"\r\nGiven second string= \"11001\"\r\nGiven third string = \"11100\"<\/pre>\n
The count of minimum bits to Flip in such a way that the XOR of A and B equals C = 4<\/pre>\n
Given number = 3\r\nGiven first string= \"110\"\r\nGiven second string= \"111\"\r\nGiven third string = \"101\"<\/pre>\n
The count of minimum bits to Flip in such a way that the XOR of A and B equals C = 1<\/pre>\n
Program to Count Minimum Bits to Flip such that XOR of A and B Equal to C in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function to say Flips_Count which takes the given three sequence\r\n# strings and the given number as the arguments and returns the count of minimum bits\r\n# to Flip in such a way that the XOR of A and B equals C.\r\n\r\n\r\ndef Flips_Count(fst_seqnce, scnd_seqnce, third_seqnce, numb):\r\n # Take a variable say totl_cnt and initialize its value to 0.\r\n totl_cnt = 0\r\n # Loop till the given number using the for loop.\r\n for itr in range(numb):\r\n # Check if the fst_seqnce[itr] == scnd_seqnce[itr] and third_seqnce[itr] == '1'\r\n # using the if conditional statement.\r\n if fst_seqnce[itr] == scnd_seqnce[itr] and third_seqnce[itr] == '1':\r\n # If the statement is true, then increment the value of above totl_cnt by 1.\r\n # Store it in the same variable.\r\n totl_cnt = totl_cnt+1\r\n\r\n # Check if the fst_seqnce[itr] != scnd_seqnce[itr] and third_seqnce[itr] == '0' using\r\n # the elif conditional statement.\r\n elif fst_seqnce[itr] != scnd_seqnce[itr] and third_seqnce[itr] == '0':\r\n # If the statement is true, then increment the value of above totl_cnt by 1.\r\n # Store it in the same variable.\r\n totl_cnt = totl_cnt+1\r\n # Return the value of totl_cnt.\r\n return totl_cnt\r\n\r\n\r\n# Give the number (which is the count of bits) as static input and store it in a variable.\r\nnumb = 5\r\n# Give the first binary sequence string as static input and store it in another variable.\r\nfst_seqnce = \"11011\"\r\n# Give the second binary sequence string as static input and store it in another variable.\r\nscnd_seqnce = \"11001\"\r\n# Give the third binary sequence string as static input and store it in another variable.\r\nthird_seqnce = \"11100\"\r\n# Pass the given three sequence strings and the given number as the arguments to the\r\n# Flips_Count function.\r\n# Print the count of minimum bits to Flip in such a way that the XOR of A and B equals C.\r\nprint(\"The count of minimum bits to Flip in such a way that the XOR of A and B equals C = \",\r\n Flips_Count(fst_seqnce, scnd_seqnce, third_seqnce, numb))\r\n<\/pre>\n
The count of minimum bits to Flip in such a way that the XOR of A and B equals C = 4<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Create a function to say Flips_Count which takes the given three sequence\r\n# strings and the given number as the arguments and returns the count of minimum bits\r\n# to Flip in such a way that the XOR of A and B equals C.\r\n\r\n\r\ndef Flips_Count(fst_seqnce, scnd_seqnce, third_seqnce, numb):\r\n # Take a variable say totl_cnt and initialize its value to 0.\r\n totl_cnt = 0\r\n # Loop till the given number using the for loop.\r\n for itr in range(numb):\r\n # Check if the fst_seqnce[itr] == scnd_seqnce[itr] and third_seqnce[itr] == '1'\r\n # using the if conditional statement.\r\n if fst_seqnce[itr] == scnd_seqnce[itr] and third_seqnce[itr] == '1':\r\n # If the statement is true, then increment the value of above totl_cnt by 1.\r\n # Store it in the same variable.\r\n totl_cnt = totl_cnt+1\r\n\r\n # Check if the fst_seqnce[itr] != scnd_seqnce[itr] and third_seqnce[itr] == '0' using\r\n # the elif conditional statement.\r\n elif fst_seqnce[itr] != scnd_seqnce[itr] and third_seqnce[itr] == '0':\r\n # If the statement is true, then increment the value of above totl_cnt by 1.\r\n # Store it in the same variable.\r\n totl_cnt = totl_cnt+1\r\n # Return the value of totl_cnt.\r\n return totl_cnt\r\n\r\n\r\n# Give the number (which is the count of bits) as user input using the int(input()) function \r\n# and store it in a variable.\r\nnumb = int(input('Enter some random number = '))\r\n# Give the first binary sequence string as user input using the input() function and \r\n# store it in another variable.\r\nfst_seqnce = input('Enter some random binary number string = ')\r\n# Give the second binary sequence string as user input using the input() function and \r\n# store it in another variable.\r\nscnd_seqnce = input('Enter some random binary number string = ')\r\n# Give the third binary sequence string as user input using the input() function and \r\n# store it in another variable.\r\nthird_seqnce = input('Enter some random binary number string = ')\r\n# Pass the given three sequence strings and the given number as the arguments to the\r\n# Flips_Count function.\r\n# Print the count of minimum bits to Flip in such a way that the XOR of A and B equals C.\r\nprint(\"The count of minimum bits to Flip in such a way that the XOR of A and B equals C = \",\r\n Flips_Count(fst_seqnce, scnd_seqnce, third_seqnce, numb))\r\n<\/pre>\n
Enter some random number = 3\r\nEnter some random binary number string = 110\r\nEnter some random binary number string = 111\r\nEnter some random binary number string = 101\r\nThe count of minimum bits to Flip in such a way that the XOR of A and B equals C = 1<\/pre>\n
\n