0

Hello,
Iv been trying to work through some issues on binary output recently. But im having problems understanding the conversion process in java when using two's complement.

I dont understand why

System.out.println(Integer.toBinaryString(-11700));

Prints out 11111111111111111101001001001100. Shoudlnt -11700 only be 15 bits long? When i calculate it by hand and invert the bits to become twos complement it produces : 101001001001100 why does java pad out the binary with 17 bits? Is this because an integer has 32 bits and only trims off non significant 0's?

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by JamesCherrill
1

ints always have 32 bits. When you convert to a String to display them the string conversion method supresses leading zeros.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.