Program to clear K-th bit of a number N

08 Nov, 2021
Given a number N, the task is to clear the K-th bit of this number N. If K-th bit is 1, then clear it to 0 and if it is 0 then leave it unchanged.Examples:  
Input: N = 5, K = 1
Output: 4
5 is represented as 101 in binary
and has its first bit 1, so clearing
it will result in 100 i.e. 4.

Input: N = 5, K = 2
Output: 5
5 is represented as 101 in binary
and has its second bit is already 0,
so clearing it will result in 101 i.e. 5.
 
Approach:  
Since bitwise AND of any bit with a reset bit results in a reset bit, i.e.
Any bit <bitwise AND> Reset bit = Reset bit

which means,
0 & 0 = 0
1 & 0 = 0
So for clearing a bit, performing a bitwise AND of the number with a reset bit is the best idea.
n = n & ~(1 << k)
OR
n &= ~(1 << k)

where k is the bit that is to be cleared
Below is the implementation of the above approach: 
C
C++
Python3
Java
C#
Javascript
// C program to clear K-th bit of a number N #include <stdio.h> // Function to clear the kth bit of nint clearBit(int n, int k){    return (n & (~(1 << (k - 1))));} // Driver codeint main(){    int n = 5, k = 1;     printf("%d\n", clearBit(n, k));     return 0;}
// C++ program to clear K-th bit of a number N #include <bits/stdc++.h>using namespace std; // Function to clear the kth bit of nint clearBit(int n, int k){    return (n & (~(1 << (k - 1))));} // Driver codeint main(){    int n = 5, k = 1;     cout<<clearBit(n, k)<<endl;     return 0;} // This code is contributed by rutvik_56.
# Python3 program to clear# K-th bit of a number N # Function to clear the kth bit of ndef clearBit(n, k):    return (n & ( ~(1 << (k - 1)))) # Driver coden = 5k = 1 print(clearBit(n, k)) # This code is contributed# by Mohit Kumar
// Java program to clear K-th bit of a number Nclass GFG{         // Function to clear the kth bit of n    static int clearBit(int n, int k)    {        return (n & (~(1 << (k - 1))));    }         // Driver code    public static void main (String[] args)    {        int n = 5, k = 1;             System.out.println(clearBit(n, k));    }} // This code is contributed by AnkitRai01
// C# program to clear K-th bit of a number Nusing System; class GFG{         // Function to clear the kth bit of n    static int clearBit(int n, int k)    {        return (n & (~(1 << (k - 1))));    }         // Driver code    public static void Main (String[] args)    {        int n = 5, k = 1;             Console.WriteLine(clearBit(n, k));    }} // This code is contributed by PrinciRaj1992
<script> // JavaScript program to clear K-th bit of a number N // Function to clear the kth bit of nfunction clearBit(n, k){    return (n & (~(1 << (k - 1))));} // Driver codevar n = 5, k = 1;document.write( clearBit(n, k)); </script>
4
 
Time Complexity: O(1)
Auxiliary Space: O(1)
mohit kumar 29
ankthon
princiraj1992
itsok
rutvik_56
subhammahato348
Numbers
Bit Magic
Bit Magic
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Builtin functions of GCC compiler
Calculate XOR from 1 to n.
Google Online Challenge 2020
Find two numbers from their sum and XOR
Highest power of 2 less than or equal to given number
Calculate square of a number without using *, / and pow()
Reverse actual bits of the given number
Count number of bits to be flipped to convert A to B
Find XOR of two number without using XOR operator