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 166 views
166 views asked Sep 29, 2023 by avibootz
1 answer 127 views
1 answer 133 views
133 views asked Sep 29, 2023 by avibootz
1 answer 119 views
119 views asked Sep 28, 2023 by avibootz
1 answer 121 views
121 views asked Feb 27, 2023 by avibootz
2 answers 196 views
...