How to turn off a particular bit in a number?
Last Updated :
28 Feb, 2023
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>
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));
Time Complexity: O(1)
Auxiliary Space: O(1)
Exercise: Write a function turnOnK() that turns the k'th bit on.
Similar Reads
How to turn on a particular bit in a number?
Given a number n and a value k, turn on the kâth bit in n.Examples: Input: n = 4, k = 2 Output: 6 Input: n = 3, k = 3 Output: 7 Input: n = 64, k = 4 Output: 72 Input: n = 64, k = 5 Output: 80 The idea is to use bitwise << and | operators. Using expression "(1 << (k â 1))â, we get a numbe
4 min read
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 all the bits in given range of a number
Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex
5 min read
Program to clear 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 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,
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
Write an Efficient C Program to Reverse Bits of a Number
Given an unsigned integer, reverse all bits of it and return the number with reversed bits. Input : n = 1Output : 2147483648 Explanation : On a machine with size of unsigned bit as 32. Reverse of 0....001 is 100....0. Input : n = 2147483648Output : 1 Recommended PracticeReverse BitsTry It!Method1 -
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
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
Binary representation of a given number
Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length.Examples: Input: n = 2Output: 00000000000000000000000000000010Input: n = 0Output: 000000000000
6 min read
Python Bin | Count total bits in a number
Given a positive number n, count total bit in it. Examples: Input : 13 Output : 4 Binary representation of 13 is 1101 Input : 183 Output : 8 Input : 4096 Output : 13 We have existing solution for this problem please refer Count total bits in a number link. Approach#1: We can solve this problem quick
3 min read