Set kth bit | Practice | GeeksforGeeks

Given a number N and a value K. From the right, set the Kth bit in the binary representation of N. The position of Least Significant Bit(or last bit) is 0, the second last bit is 1 and so on. 
Example 1:
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).

Example 2:
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).

Your Task:  
You don't need to read input or print anything. Your task is to complete the function setKthBit() which takes two integer N and K as input parameter and returns an integer after setting the K'th bit in N.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 109
0 <= K < X, where X is the number of bits in the binary representation of N.
0
yadavashish8828441 week ago
if ((N | (1<<K)) == N){           return N;       }       return (N ^ (1 << (K)));
0
bharathimp19931 week ago
Java Solution:
 
class Solution{   static int setKthBit(int N,int K){       return (N|(1<<K));   }}
0
bharathimp19931 week ago
Java Solution
0
harshpandeyalfa22 weeks ago
C++
(0.01sec)
int setKthBit(int N, int K)   {       int count=0;       int a=1;       while(1)       {           if(count==K)           {               N=N|a;               return N;           }           else           {               count++;               a=a<<1;           }       }       return 0;   }
0
madhukartemba2 weeks ago
JAVA SOLUTION:
//User function Template for Java
class Solution{
    static int setKthBit(int N,int K){
        // code here
        return N | (1<<K);
    }
}
0
alokkumar90193 weeks ago
int setKthBit(int n, int k)
   {
       // Write Your Code here
       int c=(n | (1<<k));
       return c;
   }
+1
juhisonkar5422 months ago
class Solution{   static int setKthBit(int N,int K){       // code        int bitmask=1<<K;       int ans = bitmask | N;       return ans;   }}
+2
huzefabohra5262 months ago
Solution :  One Line Code Below
Bitwise OR approach.
just make kth bit set (1) in number 0
then take OR with given N.
In  case  if kth bit is already set then  1+1=1
In case  if kth bit is 0 then also at kth bit it will do 0+1 = 1
 
//code 
 
  int setKthBit(int N, int K)   {      return N | int(pow(2,K));   }
 
0
anushkabiswas033 months ago
int setKthBit(int N, int K)   {       return (N | (1<<K)) ;   }
0
sharmanitish1833 months ago
static int setKthBit(int N,int K){      int mask=1<<K;      return N|mask;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.