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 198 views
1 answer 200 views
200 views asked Apr 6, 2024 by avibootz
1 answer 219 views
1 answer 225 views
225 views asked Apr 5, 2024 by avibootz
1 answer 249 views
249 views asked Apr 5, 2024 by avibootz
1 answer 252 views
252 views asked Apr 5, 2024 by avibootz
1 answer 243 views
...