Q: how to toggle the bits of binary representation of a number? Given a  number, i want to toggle the bits of number 'n' 
say for example if n = 6  -> 0110 
i want to get, result = 9 -> 1001 
toggle , i.e 
convert 1 to 0  & convert 0 to 1 in the binary representation 
how to do this programmatically in java, by doing XOR of num with 1's 
Thanks all for answers, so it depends of how many bits i want to toggle
say for ex, if its 8 bits then 0xff
if its entire 32 bits then oxffffffff
1 way would be n ^ ~0

A: You could do exactly that.
int y = 6^0xf

If you want to use the full int, then you have to XOR with -1 (twos complement means this is all 1's).
int y = 6^-1;

You can also use a different representation to make writing it out more intuitive.
int mask = 0xffffffff; //also -1


A:    public static void main(final String[] args) {
        final int n = 0b0110;
        System.out.println(Integer.toBinaryString(n));
        final int m = n ^ 0xffffffff;
        System.out.println(Integer.toBinaryString(m));
    }

