BigInteger b = new BigInteger("-5");
System.out.println(b.toString(2));
prints
-101
I would like to get two's complement binary code on a given number of bits instead of positive binary string with a -
sign.
For instance
BigInteger b = new BigInteger("-5");
System.out.println(b.toString(2, 4));//-5 on 4 bits in binary code
should display
1011
and
BigInteger b = new BigInteger("-5");
System.out.println(b.toString(2, 5));//-5 on 4 bits in binary code
should display
11011