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;
int turnOffK( int n, int k)
{
if (k <= 0) return n;
return (n & ~(1 << (k - 1)));
}
int main()
{
int n = 15;
int k = 4;
cout << turnOffK(n, k);
return 0;
}
|
Java
import java.io.*;
class TurnOff
{
static int turnOffK( int n, int k)
{
if (k <= 0 )
return n;
return (n & ~( 1 << (k - 1 )));
}
public static void main (String[] args)
{
int n = 15 ;
int k = 4 ;
System.out.println(turnOffK(n, k));
}
}
|
Python3
def turnOffK(n,k):
if (k < = 0 ):
return n
return (n & ~( 1 << (k - 1 )))
n = 15
k = 4
print (turnOffK(n, k))
|
C#
using System;
class GFG
{
static int turnOffK( int n, int k)
{
if (k <= 0)
return n;
return (n & ~ (1 << (k - 1)));
}
public static void Main ()
{
int n = 15;
int k = 4;
Console.Write(turnOffK(n, k));
}
}
|
PHP
<?php
function turnOffK( $n , $k )
{
if ( $k <= 0)
return $n ;
return ( $n & ~(1 << ( $k - 1)));
}
$n = 15;
$k = 4;
echo turnOffK( $n , $k );
?>
|
Javascript
<script>
function turnOffK( n, k){
if (k <= 0) return n;
return (n & ~(1 << (k - 1)));
}
let n = 15;
let k = 4;
document.write(turnOffK(n, k));
</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;
int turnOffK( int n, int k)
{
if (k <= 0)
return n;
if (n & (1 << (k - 1))) {
n = (n ^ (1 << (k - 1)));
}
return n;
}
int main()
{
int n = 15;
int k = 4;
cout << turnOffK(n, k);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int turnOffK( int n, int k)
{
if (k <= 0 )
return n;
if ((n & ( 1 << (k - 1 ))) != 0 ) {
n = (n ^ ( 1 << (k - 1 )));
}
return n;
}
public static void main(String[] args)
{
int n = 15 ;
int k = 4 ;
System.out.println(turnOffK(n, k));
}
}
|
Python3
def turn_off_k(n: int , k: int ) - > int :
if k < = 0 :
return n
if n & ( 1 << (k - 1 )):
n = (n ^ ( 1 << (k - 1 )))
return n
if __name__ = = '__main__' :
n = 15
k = 4
print (turn_off_k(n, k))
|
C#
using System;
public class GFG {
static int turnOffK( int n, int k)
{
if (k <= 0)
return n;
if ((n & (1 << (k - 1))) != 0) {
n = (n ^ (1 << (k - 1)));
}
return n;
}
static public void Main()
{
int n = 15;
int k = 4;
Console.Write(turnOffK(n, k));
}
}
|
Javascript
function turnOffK(n, k) {
if (k <= 0) {
return n;
}
if (n & (1 << (k - 1))) {
n = (n ^ (1 << (k - 1)));
}
return n;
}
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Feb, 2023
Like Article
Save Article