{"id":20427,"date":"2021-09-12T12:02:06","date_gmt":"2021-09-12T06:32:06","guid":{"rendered":"https:\/\/python-programs.com\/?p=20427"},"modified":"2021-11-22T18:36:20","modified_gmt":"2021-11-22T13:06:20","slug":"python-program-to-clear-nth-bit-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-clear-nth-bit-of-a-number\/","title":{"rendered":"Python Program to Clear nth Bit of a Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Count Number of Uppercase Letters in a String using Recursion<\/a><\/p>\n Given a number, bit position and the task is to get the number after clearing the bit at the given bit position for a given number.<\/p>\n In simple words, it converts all 1’s to 0<\/p>\n let number=7\u00a0 (111)<\/p>\n Bit position that you want to clear =1<\/p>\n It converts the 1 at index 1 to 0<\/p>\n Number after clearing = 5 (101)<\/p>\n Bitwise & Operator:<\/strong><\/p>\n If both bits are 1, sets each bit to 1.<\/p>\n Complement operator(~):<\/strong><\/p>\n The ones’ complement operator flips the bits of an integer, transforming one into zero and zero into one.<\/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 get the number after clearing the bit at the given bit position for a given number:<\/p>\n Approach:<\/strong><\/p>\n Note:<\/strong> ‘0’ indexing .Give the bit position in range(0-31).<\/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 Practice Python Program Examples<\/a> to master coding skills and learn the fundamental concepts in the dynamic programming language Python.<\/p>\n In the previous article, we have discussed Python Program to Count Number of Uppercase Letters in a String using Recursion Given a number, bit position and the task is to get the number after clearing the bit at the given bit position for a given number. In simple words, it converts all 1’s to 0 …<\/p>\nGiven Number = 60 \r\nBit position(in range 0-31)= 3<\/pre>\n
The Number after clearing the bit at the given position{ 3 } for a given number{ 60 } = 52<\/pre>\n
Given Number = 7 \r\nBit position(in range 0-31)= 1<\/pre>\n
The Number after clearing the bit at the given position{ 1 } for a given number{ 7 } = 5<\/pre>\n
Program to Clear nth Bit of a Number in Python<\/h2>\n
\n
Method #1: Using Bitwise <\/strong>&(and) Operator (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 60\r\n# Give the bit position as static input and store it in another variable.\r\nbitpositin = 3\r\n# Apply the left shift operator to 1 and the above-given bit position and\r\n# store it in another variable.\r\nnumbr_bit = (1 << bitpositin)\r\n# Apply the complement operator (which converts 0 to 1 and vice-versa) to the above result\r\n# and store it in another variable.\r\ncomplemt = (~numbr_bit)\r\n# Apply bitwise & operation for the given number and the above result and store it in\r\n# another variable say rslt_numb.\r\nrslt_numb = gvn_numb & complemt\r\n# Print the number after clearing the bit at the given position for a given number.\r\nprint(\"The Number after clearing the bit at the given position{\",\r\n bitpositin, \"} for a given number{\", gvn_numb, \"} =\", rslt_numb)\r\n<\/pre>\n
The Number after clearing the bit at the given position{ 3 } for a given number{ 60 } = 52<\/pre>\n
Method #2: Using Bitwise <\/strong>&(and) Operator (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and \r\n# store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Give the bit position as user input using the int(input()) function \r\n# and store it in another variable.\r\nbitpositin = int(input(\"Enter some random number = \"))\r\n# Apply the left shift operator to 1 and the above-given bit position and\r\n# store it in another variable.\r\nnumbr_bit = (1 << bitpositin)\r\n# Apply the complement operator (which converts 0 to 1 and vice-versa) to the above result\r\n# and store it in another variable.\r\ncomplemt = (~numbr_bit)\r\n# Apply bitwise & operation for the given number and the above result and store it in\r\n# another variable say rslt_numb.\r\nrslt_numb = gvn_numb & complemt\r\n# Print the number after clearing the bit at the given position for a given number.\r\nprint(\"The Number after clearing the bit at the given position{\",\r\n bitpositin, \"} for a given number{\", gvn_numb, \"} =\", rslt_numb)\r\n<\/pre>\n
Enter some random number = 7\r\nEnter some random number = 1\r\nThe Number after clearing the bit at the given position{ 1 } for a given number{ 7 } = 5<\/pre>\n
\n