How to remove the lowest order bit in Java

1 Answer

0 votes
public class Main {
    public static void main(String[] args) {
        int n = 2664;

        System.out.println(String.format("%16s", Integer.toBinaryString(n)).replaceAll(" ", "0"));

        n &= (n - 1);
        
        System.out.println(String.format("%16s", Integer.toBinaryString(n)).replaceAll(" ", "0"));
    }
}

  
   
   
/*
run:
  
0000101001101000
0000101001100000
   
*/

 



answered Apr 5, 2024 by avibootz

Related questions

1 answer 119 views
1 answer 113 views
113 views asked Apr 6, 2024 by avibootz
1 answer 130 views
1 answer 130 views
130 views asked Apr 5, 2024 by avibootz
1 answer 172 views
172 views asked Apr 5, 2024 by avibootz
1 answer 159 views
159 views asked Apr 5, 2024 by avibootz
1 answer 155 views
...