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

>>>>运算符允许将int和long视为32位和64位无符号整数类型,这在Java语言中是缺失的。

当你移动不代表数值的东西时,这是非常有用的。例如,您可以使用32位整数表示黑白位图图像,其中每个int在屏幕上编码32像素。如果你需要向右滚动图像,你更喜欢int左边的位成为零,这样你可以很容易地把相邻的int的位:

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;

}

上面的代码不注意上面三位的内容,因为>>>运算符使它们

没有相应的<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值