Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of the Least Significant Bit(or last bit) is 0, the second last bit is 1 and so on.Â
Examples:Â
Input: n = 10, k = 2
Output: 14
Explanation: Binary representation of the given number 10 is: 1 0 1 0, number of bits in the binary reprsentation is 4. Thus 2nd bit from right is 0. The number after changing this bit to 1 is: 14(1 1 1 0).Input: n = 15, k = 3
Output: 15
Explanation: The binary representation of the given number 15 is: 1 1 1 1, number of bits in the binary representation is 4. Thus 3rd bit from the right is 1. The number after changing this bit to 1 is 15(1 1 1 1).
Table of Content
[Naive Approach] Binary String Conversion - O(log n) Time and O(log n) Space
Convert the number to binary string, set the kth bit from right, then convert back to decimal.
[Optimal Approach] Bit Masking - O(1) Time and O(1) Space
Use left shift to create a mask with only the kth bit set, then perform OR operation with the original number.
- Create mask by shifting 1 to left by k positions
- Perform OR between mask and original number n
// C++ implementation to set the kth bit
// of the given number
#include <bits/stdc++.h>
using namespace std;
// function to set the kth bit
int setKthBit(int n, int k)
{
// kth bit of n is being set by this operation
return ((1 << k) | n);
}
// Driver program to test above
int main()
{
int n = 10, k = 2;
cout << "Kth bit set number = "
<< setKthBit(n, k);
return 0;
}
// Java implementation to set the kth bit
// of the given number
class GFG {
// function to set the kth bit
static int setKthBit(int n, int k)
{
// kth bit of n is being set by this operation
return ((1 << k) | n);
}
// Driver code
public static void main(String arg[])
{
int n = 10, k = 2;
System.out.print("Kth bit set number = "
+ setKthBit(n, k));
}
}
# Python implementation
# to set the kth bit
# of the given number
def setKthBit(n,k):
# kth bit of n is being
# set by this operation
return ((1 << k) | n)
# Driver code
n = 10
k = 2
print("Kth bit set number = ", setKthBit(n, k))
// C# implementation to set the
// kth bit of the given number
using System;
class GFG {
// function to set the kth bit
static int setKthBit(int n, int k)
{
// kth bit of n is being set
// by this operation
return ((1 << k) | n);
}
// Driver code
public static void Main()
{
int n = 10, k = 2;
Console.Write("Kth bit set number = "
+ setKthBit(n, k));
}
}
function setKthBit(n, k) {
// kth bit of n is being
// set by this operation
return ((1 << k) | n);
}
// Driver code
let n = 10;
let k = 2;
console.log("Kth bit set number = ", setKthBit(n, k));
<?php
// PHP implementation to
// set the kth bit of
// the given number
// function to set
// the kth bit
function setKthBit($n, $k)
{
// kth bit of n is being
// set by this operation
return ((1 << $k) | $n);
}
// Driver Code
$n = 10; $k = 2;
echo "Kth bit set number = ",
setKthBit($n, $k);
// This code is contributed by m_kit
?>
Output
Kth bit set number = 14