Java 位操作 - (num >>= 1) 的作用是什么?
我正在查看一些将数字输出为带有前缀 0 的二进制形式的代码。
byte number = 48;
int i = 256; //max number * 2
while( (i >>= 1) > 0) {
System.out.print(((number & i) != 0 ? "1" : "0"));
}
并且不明白 i >>= 1
的作用。我知道i >>> 1
向右移动 1 位,但不明白 =
的作用,据我所知,不可能搜索“>>=” ” 来了解它的含义。
I was looking at some code that outputs a number to the binary form with prepended 0s.
byte number = 48;
int i = 256; //max number * 2
while( (i >>= 1) > 0) {
System.out.print(((number & i) != 0 ? "1" : "0"));
}
and didn't understand what the i >>= 1
does. I know that i >> 1
shifts to the right by 1 bit but didn't understand what the =
does and as far as I know, it is not possible to do a search for ">>=" to find out what it means.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
i >>= 1
只是i = i >>> 的简写形式。 1
与i += 4
是i = i + 4
的缩写编辑:具体来说,这些都是 复合赋值运算符。
i >>= 1
is just shorhand fori = i >> 1
in the same way thati += 4
is short fori = i + 4
EDIT: Specifically, those are both examples of compound assignment operators.