栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

当您移动不代表数值的内容时,这很有用。例如,您可以使用32位

int
s
表示黑白位图图像,其中每个位图
int
在屏幕上编码32个像素。如果需要将图像向右滚动,则希望将an左侧的位
int
变为零,以便可以轻松地将相邻
int
s
的位放入:

 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; }

上面的代码没有注意高三位的内容,因为

>>>
运算符使它们成为高位

没有相应的

<<
运算符,因为对有符号和无符号数据类型的左移操作是相同的。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号