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 194 views
1 answer 194 views
194 views asked Apr 6, 2024 by avibootz
1 answer 213 views
1 answer 220 views
220 views asked Apr 5, 2024 by avibootz
1 answer 244 views
244 views asked Apr 5, 2024 by avibootz
1 answer 247 views
247 views asked Apr 5, 2024 by avibootz
1 answer 239 views
...