How to turn off a particular bit in a number?
Given a number n and a value k, turn off the k’th bit in n. Please note that k = 1 means the rightmost bit.
Examples:
Input: n = 15, k = 1 Output: 14 Input: n = 14, k = 1 Output: 14 The rightmost bit was already off, so no change. Input: n = 15, k = 2 Output: 13 Input: n = 15, k = 3 Output: 11 Input: n = 15, k = 4 Output: 7 Input: n = 15, k >= 5 Output: 15
The idea is to use bitwise <<, & and ~ operators. Using expression “~(1 << (k – 1))“, we get a number which has all bits set, except the k’th bit. If we do bitwise & of this expression with n, we get a number which has all bits same as n except the k’th bit which is 0.
Below is the implementation of above idea.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
C++
#include <iostream>using namespace std;// Returns a number that has all bits same as n// except the k'th bit which is made 0int turnOffK(int n, int k){ // k must be greater than 0 if (k <= 0) return n; // Do & of n with a number with all set bits except // the k'th bit return (n & ~(1 << (k - 1)));}// Driver program to test above functionint main(){ int n = 15; int k = 4; cout << turnOffK(n, k); return 0;} |
Java
// Java program to turn off a particular bit in a numberimport java.io.*;class TurnOff{ // Function to returns a number that has all bits same as n // except the k'th bit which is made 0 static int turnOffK(int n, int k) { // k must be greater than 0 if (k <= 0) return n; // Do & of n with a number with all set bits except // the k'th bit return (n & ~(1 << (k - 1))); } // Driver program public static void main (String[] args) { int n = 15; int k = 4; System.out.println(turnOffK(n, k)); }}// Contributed by Pramod Kumar |
Python3
# Returns a number that# has all bits same as n# except the k'th bit# which is made 0def turnOffK(n,k): # k must be greater than 0 if (k <= 0): return n # Do & of n with a number # with all set bits except # the k'th bit return (n & ~(1 << (k - 1))) # Driver coden = 15k = 4print(turnOffK(n, k))# This code is contributed# by Anant Agarwal. |
C#
// C# program to turn off a// particular bit in a numberusing System;class GFG{ // Function to returns a number // that has all bits same as n // except the k'th bit which is // made 0 static int turnOffK(int n, int k) { // k must be greater than 0 if (k <= 0) return n; // Do & of n with a number // with all set bits except // the k'th bit return (n & ~ (1 << (k - 1))); } // Driver Code public static void Main () { int n = 15; int k = 4; Console.Write(turnOffK(n, k)); }}// This code is contributed by Nitin Mittal. |
PHP
<?php// PHP program to turn off a// particular bit in a number// Returns a number that has// all bits same as n except// the k'th bit which is made 0function turnOffK($n, $k){ // k must be greater than 0 if ($k <= 0) return $n; // Do & of n with a number // with all set bits except // the k'th bit return ($n & ~(1 << ($k - 1)));}// Driver Code$n = 15;$k = 4;echo turnOffK($n, $k);// This code is contributed by nitin mittal?> |
Javascript
<script>// Returns a number that has all bits same as n// except the k'th bit which is made 0function turnOffK( n, k){ // k must be greater than 0 if (k <= 0) return n; // Do & of n with a number with all set bits except // the k'th bit return (n & ~(1 << (k - 1)));}// Driver program to test above functionlet n = 15;let k = 4;document.write(turnOffK(n, k));// This code is contributed by rohitsingh07052.</script> |
Output:
7
Exercise: Write a function turnOnK() that turns the k’th bit on.
This article is contributed by Rahul Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



