How to toggle a bit at specific position in Java

1 Answer

0 votes
public class Program {
    public static void main(String args[]) {
        int n = 365, pos = 2;
   
        System.out.println(Integer.toBinaryString(n)); 
     
        n ^= (1 << pos);
      
        System.out.println(Integer.toBinaryString(n)); 
    }
}
    
       
/*
run:

101101101
101101001
      
*/

 



answered Mar 19, 2019 by avibootz
edited Apr 2 by avibootz
...