Set Kth bit given number
Input : n = 10, k = 2 Output : 14 (10)10 = (1010)2 Now, set the 2nd bit from right. (14)10 = (1110)2 2nd bit has been set. Input : n = 15, k = 3 Output : 15 3rd bit of 15 is already set.
Code:
import java.util.*;
public class Solution {
public static void main(String[] args){
int n = 10, k=2;
//n = n | ( 1 << k );
n |= ( 1 << k );
System.out.print(n);
}
}
Output:
14
Comments
Post a Comment