{"id":10740,"date":"2021-10-01T10:30:51","date_gmt":"2021-10-01T05:00:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=10740"},"modified":"2021-11-22T18:33:27","modified_gmt":"2021-11-22T13:03:27","slug":"python-program-to-find-the-total-number-of-bits-needed-to-be-flipped","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-total-number-of-bits-needed-to-be-flipped\/","title":{"rendered":"Python Program to Find the Total Number of Bits Needed to be Flipped"},"content":{"rendered":"
Given two numbers the task is to print the number of bits to be flipped to convert the given number to the other number.<\/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 is the full approach to print the number of bits to be flipped to convert the given number to the other number.<\/p>\n Our website provided core java programs examples with output<\/a> aid beginners and expert coders to test their knowledge gap and learn accordingly.<\/p>\n This problem can be rephrased as determining the number of dissimilar corresponding bits in firstnumber and secondnumber. And the bitwise XOR operator () can assist us in determining this. The XOR of two bits returns 1 only if the bits are not the same. As a result, our solution consists of two easy steps:<\/p>\n 1. Determine the firstnumber and secondnumber’s bitwise XOR. 1. Calculate XOR of firstnumber and secondnumber. 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 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 Related Programs<\/strong>:<\/p>\n Given two numbers the task is to print the number of bits to be flipped to convert the given number to the other number. Examples: Example1: Input: given the first number = 12 given the second number = 9 Output: Binary representations of the given two numbers are : 12 = 1100 9 = 1001 …<\/p>\ngiven the first number = 12\r\ngiven the second number = 9<\/pre>\n
Binary representations of the given two numbers are :\r\n12 = 1100\r\n9 = 1001\r\nThe total number of bits to be flipped = 2<\/pre>\n
given the first number = 59\r\ngiven the second number = 3<\/pre>\n
Binary representations of the given two numbers are :\r\n59 = 111011\r\n3 = 11\r\nThe total number of bits to be flipped = 3<\/pre>\n
Program to Find the Total Number of Bits Needed to be Flipped<\/h2>\n
\n
1)Algorithm<\/h3>\n
\n2. Determine the number of set bits in the result.<\/p>\n
\nfirstnumber _xor_secondnumber = firstnumber ^ secondnumber
\n2. Count the set bits in the above
\ncalculated XOR result.
\ncountSetBits ( firstnumber _xor_secondnumber)<\/p><\/blockquote>\n2)Using XOR Operator and Count Variable(Static Input)<\/h3>\n
\n
# function which accepts the two numbers as arguments\r\n# and return the number of bits to be flipped\r\n\r\n\r\ndef countFlipbits(firstnumber, secondnumber):\r\n # Calculate the xor value of the two numbers and store it in a result say resultnum.\r\n resultnum = firstnumber ^ secondnumber\r\n # Use the while loop to calculate the total number of set bits in the resultnum.\r\n setbitCounter = 0\r\n while resultnum:\r\n resultnum = resultnum & (resultnum - 1)\r\n setbitCounter = setbitCounter + 1\r\n # Return the number of set bits which is the result (number of bits to be flipped).\r\n return setbitCounter\r\n\r\n\r\n# Driver Code\r\n# Give the two numbers as static input and store them in two separate variables.\r\nfirstnumber = 12\r\nsecondnumber = 9\r\n# Pass the given two numbers as arguments to the ( function which calculates the total number\r\n# of bits to be flipped to convert one number into another.\r\nprint('Binary representations of the given two numbers are :')\r\nprint(firstnumber, '=', bin(firstnumber)[2:])\r\nprint(secondnumber, '=', bin(secondnumber)[2:])\r\nprint('The total number of bits to be flipped =',\r\n countFlipbits(firstnumber, secondnumber))\r\n<\/pre>\n
Binary representations of the given two numbers are :\r\n12 = 1100\r\n9 = 1001\r\nThe total number of bits to be flipped = 2<\/pre>\n
3)Using XOR Operator and Count Variable(User Input separated by spaces)<\/h3>\n
\n
# function which accepts the two numbers as arguments\r\n# and return the number of bits to be flipped\r\n\r\n\r\ndef countFlipbits(firstnumber, secondnumber):\r\n # Calculate the xor value of the two numbers and store it in a result say resultnum.\r\n resultnum = firstnumber ^ secondnumber\r\n # Use the while loop to calculate the total number of set bits in the resultnum.\r\n setbitCounter = 0\r\n while resultnum:\r\n resultnum = resultnum & (resultnum - 1)\r\n setbitCounter = setbitCounter + 1\r\n # Return the number of set bits which is the result (number of bits to be flipped).\r\n return setbitCounter\r\n\r\n\r\n# Driver Code\r\n# Give the two numbers as user input using int, map(), and split() functions.\r\n# Store them in two separate variables.\r\nfirstnumber, secondnumber = map(int, input(\r\n 'Enter some random two numbers separated by spaces = ').split())\r\n# Pass the given two numbers as arguments to the ( function which calculates the total number\r\n# of bits to be flipped to convert one number into another.\r\nprint('Binary representations of the given two numbers are :')\r\nprint(firstnumber, '=', bin(firstnumber)[2:])\r\nprint(secondnumber, '=', bin(secondnumber)[2:])\r\nprint('The total number of bits to be flipped =',\r\n countFlipbits(firstnumber, secondnumber))\r\n<\/pre>\n
Enter some random two numbers separated by spaces = 54 96\r\nBinary representations of the given two numbers are :\r\n54 = 110110\r\n96 = 1100000\r\nThe total number of bits to be flipped = 4<\/pre>\n
4)Using XOR Operator and Built-in Count function(Static Input)<\/h3>\n
\n
# function which accepts the two numbers as arguments\r\n# and return the number of bits to be flipped\r\n\r\n\r\ndef countFlipbits(firstnumber, secondnumber):\r\n # Calculate the xor value of the two numbers and store it in a result say resultnum.\r\n resultnum = firstnumber ^ secondnumber\r\n # Convert this resultnum to binary and calculate the total\r\n # number of sets bits in it using the count() function.\r\n binNumber = bin(resultnum)\r\n setbitCounter = binNumber.count('1')\r\n return setbitCounter\r\n\r\n\r\n# Driver Code\r\n\r\n# Driver Code\r\n# Give the two numbers as static input and store them in two separate variables.\r\nfirstnumber = 18\r\nsecondnumber = 68\r\n# Pass the given two numbers as arguments to the ( function which calculates the total number\r\n# of bits to be flipped to convert one number into another.\r\nprint('Binary representations of the given two numbers are :')\r\nprint(firstnumber, '=', bin(firstnumber)[2:])\r\nprint(secondnumber, '=', bin(secondnumber)[2:])\r\nprint('The total number of bits to be flipped =',\r\n countFlipbits(firstnumber, secondnumber))\r\n<\/pre>\n
Binary representations of the given two numbers are :\r\n18 = 10010\r\n68 = 1000100\r\nThe total number of bits to be flipped = 4<\/pre>\n
5)Using XOR Operator and Built-in Count function(User Input Separated by Newline)<\/h3>\n
\n
# function which accepts the two numbers as arguments\r\n# and return the number of bits to be flipped\r\n\r\n\r\ndef countFlipbits(firstnumber, secondnumber):\r\n # Calculate the xor value of the two numbers and store it in a result say resultnum.\r\n resultnum = firstnumber ^ secondnumber\r\n # Convert this resultnum to binary and calculate the total\r\n # number of sets bits in it using the count() function.\r\n binNumber = bin(resultnum)\r\n setbitCounter = binNumber.count('1')\r\n return setbitCounter\r\n\r\n\r\n# Driver Code\r\n\r\n# Driver Code\r\n# Give the first number as user input using int(input()) and store it in a variable.\r\nfirstnumber = int(input('Enter first random number = '))\r\n# Give the second number as user input using int(input()) and store it in another variable.\r\nsecondnumber = int(input('Enter second random number = '))\r\n# Pass the given two numbers as arguments to the ( function which calculates the total number\r\n# of bits to be flipped to convert one number into another.\r\nprint('Binary representations of the given two numbers are :')\r\nprint(firstnumber, '=', bin(firstnumber)[2:])\r\nprint(secondnumber, '=', bin(secondnumber)[2:])\r\nprint('The total number of bits to be flipped =',\r\n countFlipbits(firstnumber, secondnumber))\r\n<\/pre>\n
Enter first random number = 59\r\nEnter second random number = 3\r\nBinary representations of the given two numbers are :\r\n59 = 111011\r\n3 = 11\r\nThe total number of bits to be flipped = 3<\/pre>\n
\n