简体   繁体   中英

Shifting bits, where did it came from?

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?

There are no leading zeroes in the returned String from Integer.toBinaryString . There are 16 1 s in 0xFFFF , but there are 16 0 s 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM