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:

13

Comments

Popular posts from this blog

Rearrange Array in Maximum-Minimum form

Second Largest Element

Check if a number is a power of another number