该>>>
运营商允许你将int
和long
为32位和64位 无符号 整型,这是从Java语言缺少的。@H_404_9@
当您移动不代表数值的内容时,这很有用。例如,您可以使用32位int
s 表示黑白位图图像,其中每个位图int
在屏幕上编码32个像素。如果需要将图像向右滚动,则希望将an左侧的位int
变为零,以便可以轻松地将相邻int
s 的位放入:@H_404_9@
int shiftBy = 3;
int[] imageRow = ...
int shiftCarry = 0;
// The last shiftBy bits are set to 1, the remaining ones are zero
int mask = (1 << shiftBy)-1;
for (int i = 0 ; i != imageRow.length ; i++) {
// Cut out the shiftBits bits on the right
int nextCarry = imageRow & mask;
// Do the shift, and move in the carry into the freed upper bits
imageRow[i] = (imageRow[i] >>> shiftBy) | (carry << (32-shiftBy));
// Prepare the carry for the next iteration of the loop
carry = nextCarry;
}
上面的代码没有注意高三位的内容,因为>>>
运算符使它们成为高位@H_404_9@
没有相应的<<
运算符,因为对有符号和无符号数据类型的左移操作是相同的。@H_404_9@