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 183 views
183 views asked Sep 29, 2023 by avibootz
1 answer 143 views
1 answer 154 views
154 views asked Sep 29, 2023 by avibootz
1 answer 128 views
128 views asked Sep 28, 2023 by avibootz
1 answer 137 views
137 views asked Feb 27, 2023 by avibootz
2 answers 212 views
...