什么是无符号向右移位运算符&QUOT的目的;>>>"在Java中? [英] What is the purpose of the unsigned right shift operator ">>>" in Java?

查看:113
本文介绍了什么是无符号向右移位运算符&QUOT的目的;>>>"在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白在Java中的无符号向右移位运算符>>>的做法,但我们为什么需要它,为什么我们并不需要一个相应的无符号左移位运算符?

I understand what the unsigned right shift operator ">>>" in Java does, but why do we need it, and why do we not need a corresponding unsigned left shift operator?

推荐答案

>>> 操作符可以让你把 INT 为32位和64位的符号的整数类型,这是Java语言缺少的。

The >>> operator lets you treat int and long as 32- and 64-bit unsigned integral types, which are missing from the Java language.

当你转移的东西,不重present一个数值,这是非常有用的。例如,您可以重新present使用32位 INT S,黑白位图图像,其中每个 INT 连接$ C在屏幕上$ 32的CS像素。如果您需要将图像向右滚动,你会preFER位上的左侧有一个 INT 变成零,这样你可以很容易地把位从相邻 INT 取值:

This is useful when you shift something that does not represent a numeric value. For example, you could represent a black and white bit map image using 32-bit ints, where each int encodes 32 pixels on the screen. If you need to scroll the image to the right, you would prefer the bits on the left of an int to become zeros, so that you could easily put the bits from the adjacent ints:

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

在code以上不注重高三位的内容,因为&GT;&GT;&GT; 运营商,使他们

有没有相应的&LT;&LT; 运营商因为符号和无符号的数据类型是相同的左移位操作

There is no corresponding << operator because left-shift operations on signed and unsigned data types are identical.

这篇关于什么是无符号向右移位运算符&QUOT的目的;&GT;&GT;&GT;&QUOT;在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆