How to invert the bits of a number in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        int x = 981;

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

	    x = ~x;

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




/*
run:
 
0000001111010101
11111111111111111111110000101010
 
*/

 



answered Sep 28, 2023 by avibootz

Related questions

2 answers 292 views
292 views asked Sep 29, 2023 by avibootz
1 answer 216 views
1 answer 226 views
226 views asked Sep 29, 2023 by avibootz
1 answer 210 views
210 views asked Sep 28, 2023 by avibootz
1 answer 217 views
217 views asked Feb 27, 2023 by avibootz
2 answers 322 views
...