Open In App

How to turn off a particular bit in a number?

Last Updated : 28 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number n and a value k, turn off the kth 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 the expression "~(1 << (k - 1))", we get a number that has all bits set, except the kth bit. If we do bitwise & of this expression with n, we get a number that has all bits the same as n except the kth bit which is 0. 

Below is the implementation of the above idea. 

C++
#include <iostream>
using namespace std;

// Returns a number that has all bits same as n
// except the k'th bit which is made 0
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 to test above function
int main()
{
    int n = 15;
    int k = 4;
    cout << turnOffK(n, k);
    return 0;
} 
Java
// Java program to turn off a particular bit in a number
import 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 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 code
n = 15
k = 4
print(turnOffK(n, k))

# This code is contributed
# by Anant Agarwal.
C#
// C# program to turn off a 
// particular bit in a number
using 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 0
function 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 0
function 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 function
let n = 15;
let k = 4;
document.write(turnOffK(n, k));

// This code is contributed by rohitsingh07052.
</script>

Output
7

Time Complexity: O(1)
Auxiliary Space:  O(1)
 

Method 2: Using XOR operator.

Left shift 1 by (k - 1) times and check if kth bit is set or not, if set then take XOR for togging the kth bit.

Below is the implementation of the above approach:

C++
#include <iostream>
using namespace std;

// Returns a number that has all bits same as n
// except the k'th bit which is made 0
int turnOffK(int n, int k)
{
    // k must be greater than 0
    if (k <= 0)
        return n;

    // Check if the kth bit is set or not
    if (n & (1 << (k - 1))) {

        // Toggle the kth bit
        n = (n ^ (1 << (k - 1)));
    }

    return n;
}

// Driver program to test above function
int main()
{
    int n = 15;
    int k = 4;
    cout << turnOffK(n, k);
    return 0;
}

// This code is contributed by hkdass001
Java
// Java implementation of the approach

import java.util.*;

public class GFG {
    // 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;

        // Check if the kth bit is set or not
        if ((n & (1 << (k - 1))) != 0) {

            // Toggle the kth bit
            n = (n ^ (1 << (k - 1)));
        }

        return n;
    }

    // Driver program to test above function
    public static void main(String[] args)
    {
        int n = 15;
        int k = 4;
        System.out.println(turnOffK(n, k));
    }
}

// This code is contributed by Karandeep1234
Python3
def turn_off_k(n: int, k: int) -> int:
    # k must be greater than 0
    if k <= 0:
        return n
    
    # Check if the kth bit is set or not
    if n & (1 << (k - 1)):
      
        # Toggle the kth bit
        n = (n ^ (1 << (k - 1)))
    
    return n

# Driver program to test above function
if __name__ == '__main__':
    n = 15
    k = 4
    print(turn_off_k(n, k))
C#
// C# implementation of the approach
using System;

public class GFG {

    // 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;

        // Check if the kth bit is set or not
        if ((n & (1 << (k - 1))) != 0) {

            // Toggle the kth bit
            n = (n ^ (1 << (k - 1)));
        }

        return n;
    }
    // Driver Code
    static public void Main()
    {
        int n = 15;
        int k = 4;
        Console.Write(turnOffK(n, k));
        // Code
    }
}
// This code is contributed by Akshay
// Tripathi(akshaytripathi19410)_
JavaScript
function turnOffK(n, k) {
    // k must be greater than 0
    if (k <= 0) {
        return n;
    }

    // Check if the kth bit is set or not
    if (n & (1 << (k - 1))) {
        // Toggle the kth bit
        n = (n ^ (1 << (k - 1)));
    }

    return n;
}

// Driver program to test above function
let n = 15;
let k = 4;
console.log(turnOffK(n, k));

Output
7

Time Complexity: O(1)
Auxiliary Space: O(1)
 

Exercise: Write a function turnOnK() that turns the k'th bit on.


Next Article
Article Tags :
Practice Tags :

Similar Reads