{"id":20531,"date":"2021-09-15T19:50:44","date_gmt":"2021-09-15T14:20:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=20531"},"modified":"2021-11-22T18:36:18","modified_gmt":"2021-11-22T13:06:18","slug":"python-program-to-toggle-the-last-m-bits","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-toggle-the-last-m-bits\/","title":{"rendered":"Python Program to Toggle the Last m Bits"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Count Minimum Bits to Flip such that XOR of A and B Equal to C<\/a><\/p>\n Given a number n, the task is to toggle the last m bits of the given number in its binary representation.<\/p>\n Toggling:\u00a0<\/strong><\/p>\n A toggling operation changes the value of a bit from 0 to 1 and from 1 to 0.<\/p>\n let Given number =30 m=3<\/p>\n The binary representation of 30=11110<\/p>\n Binary representation after toggling the last 3 bits is =11001<\/p>\n Decimal equivalent after toggling =25<\/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 toggle the given last m bits of a given number 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 The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners<\/a>. Using them you can develop code on your own and master coding skills.<\/p>\n In the previous article, we have discussed Python Program to Count Minimum Bits to Flip such that XOR of A and B Equal to C Given a number n, the task is to toggle the last m bits of the given number in its binary representation. Toggling:\u00a0 A toggling operation changes the value of a …<\/p>\nGiven Number = 30\r\nGiven m value = 3<\/pre>\n
The given number{ 30 } after toggling the given last m { 3 } bits = 25<\/pre>\n
Given Number = 45\r\nGiven m value = 2<\/pre>\n
The given number{ 45 } after toggling the given last m { 2 } bits = 46<\/pre>\n
Program to Toggle the Last m Bits in Python<\/h2>\n
\n
Method #1: Using Xor(^) Operator (Static Input)<\/h3>\n
\n
# Create a\u00a0 function to say toglng_lstmbits which takes the given number, m value as the\r\n# arguments and returns the number after toggling the given last m bits.\r\n\r\n\r\ndef toglng_lstmbits(gvn_numb, m):\r\n # Apply the left shift operator to 1 and the above-given m value and subtract 1 from it.\r\n # Store it in another variable.\r\n fnl_numbr = (1 << m) - 1\r\n # Return the XOR value of the given number and the above result.\r\n return (gvn_numb ^ fnl_numbr)\r\n\r\n\r\n# Give the number as static input and store it in a variable.\r\ngvn_numb = 30\r\n# Give the value of m as static input and store it in another variable.\r\nm = 3\r\n# Pass the given number, m value as the arguments to the toglng_lstmbits function.\r\n# Print the number after toggling the given last m bits.\r\nprint(\"The given number{\", gvn_numb, \"} after toggling the given last m {\",\r\n m, \"} bits = \", toglng_lstmbits(gvn_numb, m))\r\n<\/pre>\n
The given number{ 30 } after toggling the given last m { 3 } bits = 25<\/pre>\n
Method #2: Using Xor(^) Operator (User Input)<\/h3>\n
\n
# Create a\u00a0 function to say toglng_lstmbits which takes the given number, m value as the\r\n# arguments and returns the number after toggling the given last m bits.\r\n\r\n\r\ndef toglng_lstmbits(gvn_numb, m):\r\n # Apply the left shift operator to 1 and the above-given m value and subtract 1 from it.\r\n # Store it in another variable.\r\n fnl_numbr = (1 << m) - 1\r\n # Return the XOR value of the given number and the above result.\r\n return (gvn_numb ^ fnl_numbr)\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_numb = int(input(\"Enter some random number = \"))\r\n# Give the value of m as user input using the int(input()) function and \r\n# store it in another variable.\r\nm = int(input(\"Enter some random number = \"))\r\n# Pass the given number, m value as the arguments to the toglng_lstmbits function.\r\n# Print the number after toggling the given last m bits.\r\nprint(\"The given number{\", gvn_numb, \"} after toggling the given last m {\",\r\n m, \"} bits = \", toglng_lstmbits(gvn_numb, m))\r\n<\/pre>\n
Enter some random number = 45\r\nEnter some random number = 2\r\nThe given number{ 45 } after toggling the given last m { 2 } bits = 46<\/pre>\n
\n