简体   繁体   English

移位位,它是从哪里来的?

[英]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? 我的问题是, 0是从哪里来的?

There are no leading zeroes in the returned String from Integer.toBinaryString . Integer.toBinaryString返回的String中没有前导零。 There are 16 1 s in 0xFFFF , but there are 16 0 s too. 0xFFFF有16 1 s,但也有16 0 s。

00000000 00000000 11111111 11111111  // 16 printed

Then the left shift by 1 made a zero significant. 然后左移1表示零。

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 . 此值将转换为二进制(以2为底)的ASCII数字字符串, 且没有多余的前导0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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