H Melua

Ranch Hand

Posts: 172

posted 10 years ago

First of all, parse and println works in decimal, so it will not treat what you have as binary nor will it print out as binary, in the way that you have it written. Anyway...

100 (decimal) ==> 0110 0100 (binary)

shift left by 2 ==> 1001 0000

this is a negative number due to the sign bit, we can take a twos complement to see what it is...

~x = 0110 1111

~x + 1 = -x = 0111 0000 = 112

hence, x = -112

Henry

100 (decimal) ==> 0110 0100 (binary)

shift left by 2 ==> 1001 0000

this is a negative number due to the sign bit, we can take a twos complement to see what it is...

~x = 0110 1111

~x + 1 = -x = 0111 0000 = 112

hence, x = -112

Henry

posted 10 years ago

If you want to parse the number as binary, use Byte.parseByte("00000100", 2).

When printing, use Integer.toBinaryString instead.

Also note that you can never get 1000 - you shift by two so you would get 10000.

When printing, use Integer.toBinaryString instead.

Also note that you can never get 1000 - you shift by two so you would get 10000.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions