How to turn of a particular bit in a number
Input: n=15, k=1
Output: 14
Explanation:
15 -> 1111 & ~(0001) -> 1111 & 1110 -> 14
Code:
import java.util.*;
public class Solution {
public static void main(String[] args){
int n = 15, k=2;
n = n & ( ~( 1 << (k-1) ) );
System.out.print(n);
}
}
Output:
Output: 14
Explanation:
15 -> 1111 & ~(0001) -> 1111 & 1110 -> 14
Code:
import java.util.*;
public class Solution {
public static void main(String[] args){
int n = 15, k=2;
n = n & ( ~( 1 << (k-1) ) );
System.out.print(n);
}
}
Output:
13
Comments
Post a Comment