Program to clear K-th bit of a number N Last Updated : 08 Nov, 2021 Comments Improve Suggest changes Like Article Like Report 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 = 0So 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 program to clear K-th bit of a number N #include <stdio.h> // Function to clear the kth bit of n int clearBit(int n, int k) { return (n & (~(1 << (k - 1)))); } // Driver code int main() { int n = 5, k = 1; printf("%d\n", clearBit(n, k)); return 0; } C++ // 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 n int clearBit(int n, int k) { return (n & (~(1 << (k - 1)))); } // Driver code int main() { int n = 5, k = 1; cout<<clearBit(n, k)<<endl; return 0; } // This code is contributed by rutvik_56. Python3 # Python3 program to clear # K-th bit of a number N # Function to clear the kth bit of n def clearBit(n, k): return (n & ( ~(1 << (k - 1)))) # Driver code n = 5 k = 1 print(clearBit(n, k)) # This code is contributed # by Mohit Kumar Java // Java program to clear K-th bit of a number N 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; System.out.println(clearBit(n, k)); } } // This code is contributed by AnkitRai01 C# // C# program to clear K-th bit of a number N using 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 JavaScript <script> // JavaScript program to clear K-th bit of a number N // Function to clear the kth bit of n function clearBit(n, k) { return (n & (~(1 << (k - 1)))); } // Driver code var n = 5, k = 1; document.write( clearBit(n, k)); </script> Output: 4 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to clear K-th bit of a number N C code_r Follow Improve Article Tags : Bit Magic DSA Numbers Practice Tags : Bit MagicNumbers Similar Reads Program to toggle K-th bit of a number N Given a number N, the task is to clear the K-th bit of this number N. If K-th bit is 0, then set it to 1 and if it is 1 then set it to 0. Examples: Input: N = 5, K = 2 Output: 7 5 is represented as 101 in binary and has its second bit 0, so toggling it will result in 111 i.e. 7. Input: N = 5, K = 1 3 min read Set the K-th bit of a given number Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.Examples: Input : n = 10, k = 2 Output : 1 4 min read Toggling k-th bit of a number For a given number n, if k-th bit is 0, then toggle it to 1 and if it is 1 then, toggle it to 0.Examples : Input : n = 6, k = 1Output : 76 is represented as 110 in binary and has its first bit 0, so toggling it will result in 111 i.e. 7.Input : n = 2, k = 3Output : 62 is represented as 010 in binary 3 min read Position of the K-th set bit in a number Given two numbers N and K, The task is to find the index of the K-th set bit in the number from the right. Note: Indexing in the binary representation starts from 0 from the right. For example in the binary number "000011", the first set bit is at index 0 from the right, and the second set bit is at 6 min read Print 'K'th least significant bit of a number A number N is given. We need to print its 'K'th Least Significant Bit.Examples : Input : num = 10, k = 4 Output : 1 Explanation : Binary Representation of 10 is 1010. 4th LSB is 1. Input : num = 16, k = 3 Output : 0 Explanation : Binary Representation of 16 is 10000. 3rd LSB is 0.Recommended Practic 3 min read Like