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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.