您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Java中无符号右移运算符“ >>>”的目的是什么?

Java中无符号右移运算符“ >>>”的目的是什么?

>>>运营商允许你将intlong为32位和64位 无符号 整型,这是从Java语言缺少的。@H_404_9@

当您移动不代表数值的内容时,这很有用。例如,您可以使用32位ints 表示黑白位图图像,其中每个位图int在屏幕上编码32个像素。如果需要将图像向右滚动,则希望将an左侧的位int变为零,以便可以轻松地将相邻ints 的位放入:@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@

java 2022/1/1 18:15:38 有600人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶