Replace the Kth Bit From the Left

Last Updated : 29 Jun, 2026

Given two numbers n and k, change the kth bit (counted from the left) of n's binary representation to 0 if it is currently 1. If that bit is already 0, or if k exceeds the total number of bits in n, return n unchanged.

Examples: 

Input: n = 13, k = 2
Output: 9
Explanation: Binary of 13 is 1101. The 2nd bit from the left is 1, so it becomes 0, giving 1001 = 9.

Input: n = 13, k = 6
Output: 13
Explanation: Binary of 13 is 1101, which only has 4 bits. There's no 6th bit from the left, so n is returned unchanged.

Try It Yourself
redirect icon

Using Bit Length Calculation and Bitmask - O(log n) Time and O(1) Space

The idea is to first find how many bits n actually has, by repeatedly right-shifting it until it becomes 0. Once we know the bit length, the kth bit from the left can be translated into its equivalent position from the right. We then use a bitmask to check whether that bit is set, and if so, clear it using a bitwise AND with the mask's complement.

Step by Step Implementation:

  • Find the bit length of n by right-shifting it until it reaches 0.
  • If k exceeds the bit length, return n unchanged.
  • Convert k (from the left) to the equivalent position from the right.
  • Build a mask with only that bit set, using 1 << bitPos.
  • If the bit is set, clear it using n & ~mask; otherwise, return n unchanged.
C++
#include <iostream>
using namespace std;

int replaceBit(int n, int k) {
    
    // Find the bit length of n by right-shifting until it becomes 0
    int len = 0, temp = n;
    while (temp > 0) { len++; temp >>= 1; }

    // If k exceeds the bit length, there's no such bit -- return n unchanged
    if (k > len) return n;

    // Convert k (from the left) into the equivalent position from the right
    int bitPos = len - k;

    // If that bit is set, clear it using a bitmask; otherwise return n as-is
    if (n & (1 << bitPos)) {
        return n & ~(1 << bitPos);
    }
    return n;
}

int main() {
    int n = 13, k = 2;
    cout << replaceBit(n, k) << endl;
    return 0;
}
Java
class GFG {
    static int replaceBit(int n, int k) {
        
        // Find the bit length of n by right-shifting until it becomes 0
        int len = 0, temp = n;
        while (temp > 0) { len++; temp >>= 1; }

        // If k exceeds the bit length, there's no such bit -- return n unchanged
        if (k > len) return n;

        // Convert k (from the left) into the equivalent position from the right
        int bitPos = len - k;

        // If that bit is set, clear it using a bitmask; otherwise return n as-is
        if ((n & (1 << bitPos)) != 0) {
            return n & ~(1 << bitPos);
        }
        return n;
    }

    public static void main(String[] args) {
        int n = 13, k = 2;
        System.out.println(replaceBit(n, k));
    }
}
Python
def replaceBit(n, k):
    
    # Find the bit length of n by right-shifting until it becomes 0
    length = 0
    temp = n
    while temp > 0:
        length += 1
        temp >>= 1

    # If k exceeds the bit length, there's no such bit -- return n unchanged
    if k > length:
        return n

    # Convert k (from the left) into the equivalent position from the right
    bit_pos = length - k

    # If that bit is set, clear it using a bitmask; otherwise return n as-is
    if n & (1 << bit_pos):
        return n & ~(1 << bit_pos)
    return n

n, k = 13, 2
print(replaceBit(n, k))
C#
using System;

class GFG {
    static int replaceBit(int n, int k) {
        
        // Find the bit length of n by right-shifting until it becomes 0
        int len = 0, temp = n;
        while (temp > 0) { len++; temp >>= 1; }

        // If k exceeds the bit length, there's no such bit -- return n unchanged
        if (k > len) return n;

        // Convert k (from the left) into the equivalent position from the right
        int bitPos = len - k;

        // If that bit is set, clear it using a bitmask; otherwise return n as-is
        if ((n & (1 << bitPos)) != 0) {
            return n & ~(1 << bitPos);
        }
        return n;
    }

    static void Main() {
        int n = 13, k = 2;
        Console.WriteLine(replaceBit(n, k));
    }
}
JavaScript
function replaceBit(n, k) {
    
    // Find the bit length of n by right-shifting until it becomes 0
    let len = 0, temp = n;
    while (temp > 0) { len++; temp >>= 1; }

    // If k exceeds the bit length, there's no such bit -- return n unchanged
    if (k > len) return n;

    // Convert k (from the left) into the equivalent position from the right
    const bitPos = len - k;

    // If that bit is set, clear it using a bitmask; otherwise return n as-is
    if (n & (1 << bitPos)) {
        return n & ~(1 << bitPos);
    }
    return n;
}

// Driver Code
const n = 13, k = 2;
console.log(replaceBit(n, k));

Output
9
Comment