I have this code snippet:
String negativeBinary = "";
negativeBinary = Integer.toBinaryString(decimal);
System.out.println(negativeBinary);
which gives an answer of:11111111111111111111111111111011 when -5 is entered.
Is there anyway to cut off the leading 1’s to limit the answer to say just 10 digits?
Thanks.
Sure, but note that the result returned is the correct 32-bit 2’s-complement representation of -5. If you just chop off the leading part, it won’t be correct.
This will chop off everything but the last 10 characters: