Question

int a=0xFFFF; 
        System.out.println("test1 "+Integer.toBinaryString(a)); 
        a<<=1;
        System.out.println("test2 "+Integer.toBinaryString(a)); 
Output:
test1 1111111111111111
test2 11111111111111110

My question is, where did the 0 came from?

Était-ce utile?

La solution

There are no leading zeroes in the returned String from Integer.toBinaryString. There are 16 1s in 0xFFFF, but there are 16 0s too.

00000000 00000000 11111111 11111111  // 16 printed

Then the left shift by 1 made a zero significant.

00000000 00000001 11111111 11111110  // 17 printed

This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top