How to turn off a particular bit in a number?

23 Jun, 2022
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. 
C++
Java
Python3
C#
PHP
Javascript
#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 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
# Returns a number that# has all bits same as n# except the k'th bit# which is made 0 def 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# 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 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?>
<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>
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
default, selected
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
7
Time Complexity: O(1)Auxiliary Space:  O(1) 
How to turn off a particular bit in a number? | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersHow to turn off a particular bit in a number? | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:57•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=tGpBOeBtr7w" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
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 
nitin mittal
rohitsingh07052
ranjanrohit840
hardikkoriintern
Microsoft
Bit Magic
Strings
Microsoft
Strings
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Program to find whether a given number is power of 2
Binary representation of a given number
Josephus problem | Set 1 (A O(n) Solution)
Divide two integers without using multiplication, division and mod operator
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Different Methods to Reverse a String in C++