移位运算符操作数是负数的处理方式

i<<-1和i<<31 的结果一样
1 “-1”表示成补码是1111 ….11 1111 ,31是 0000 …0001 1111,,他们的后五位是一样的。
2 Interger的移位运算只注意后5位,看注释
Note also that rotation by any multiple of 32 is a no-op, so all but the last five bits of the rotation distance can be ignored, even if the distance is negative:
总结:将负数写为补码,这个数的低5位便是其真正移位的数字

发布了55 篇原创文章 · 获赞 29 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/ruibin_cao/article/details/96831569
今日推荐