{"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

Given Number = 60 \r\nBit position(in range 0-31)= 3<\/pre>\n

Output:<\/strong><\/p>\n

The Number after clearing the bit at the given position{ 3 } for a given number{ 60 } = 52<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Number = 7 \r\nBit position(in range 0-31)= 1<\/pre>\n

Output:<\/strong><\/p>\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

Below are the ways to get the number after clearing the bit at the given bit position for a given number:<\/p>\n