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?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top