现在的位置: 首页 > 综合 > 正文

移位操作符(Thinking in Java 4th Edition)

2014年01月17日 ⁄ 综合 ⁄ 共 2506字 ⁄ 字号 评论关闭

今天看到Thinking in Java 4th Edition的3.11节--移位操作符,其中有些地方有些迷茫,经过自己的思考和查找资料,弄明白了。下面记录下来,以备后用。

3.11 Shift operators

The shift operators also manipulate bits. They can be used solely with primitive, integral types.1) The left-shift operator (<<) produces the operand to the left of the operator after it has been shifted to the left by the number of bits specified to
the right of the operator (inserting zeroes at the lower-order bits).
The signed right-shift operator (>>) produces the operand to the left of the operator after it has been shifted to the right by the number of bits specified to the right of the operator.
The signed right shift >> uses sign extension: If the value is positive, zeroes are inserted at the higher-order bits; if the value is negative, ones are inserted at the higher-order bits. Java has also added the unsigned right shift >>>, which uses zero extension:
Regardless of the sign, zeroes are inserted at the higher-order bits. This operator does not exist in C or C++.

1)  这句话够不好理解的,译文中是这样说的:左移操作符(<<)能按照操作符右侧指定的位数将操作符左边的操作数向左移动(在低位补0)。   

     下面是自己的分析

     The left-shift operator (<<) produces operand to the left of the operator。produce和to是结合起来的,左移操作符(<<)产生操作数放到操作符的左边。

     by the number of bits specified to the right by the number of bits specified to the right of the operator. 按照操作符右侧指定的位数。

-----------------------------------------------------------------------------------------------------------------------------------------

If you shift a char, byte, or short, it will be promoted to int before the shift takes place, and the result will be an int.2) Only the five low-order bits of the right-hand side will be used.This prevents you from shifting more than the number of bits
in an int.
If you’re operating on a long, you’ll get a long result. Only the six low-order bits of the right-hand side will be used, so you can’t shift more than the number of bits in a long.

2) 只有数值右端的低5位才有用。这样可以防止移位超过int型值所具有的位数。我的理解如下:因为int类型是4个字节,32位,低5位最大为31(二进制为:11111),如果只有低5位有用,那么其实就是对操作符右边的数对32求模,例如33%32=1,等于只移动了1位,而32%32=0,等于没有移动,所以你永远不可能移动超过31位。而对long类型,因为long类型是8字节,64位,低6位最大为63(二进制为:111111),则低6位有用,移位才会不超出long型的范围。

-----------------------------------------------------------------------------------------------------------------------------------------

Shifts can be combined with the equal sign (<<= or >>= or >>>=). The lvalue is replaced by the lvalue shifted by the rvalue.3) There is a problem, however, with the unsigned right shift combined with assignment. If you use it with byte or short, you don’t
get the correct results.
Instead, these are promoted to int and right shifted, but then truncated as they are assigned back into their variables, so you get -1 in those cases. The following example demonstrates this:

这段话意思是说,移位操作符可以和等号(=)组合使用,但是对byte或short在结合赋值操作进行无符号右移时会出现问题,得到的可能不是正确的结果。因为先被转换成int类型,在经右移操作,然后被截断,赋值给原来的类型,在这种情况下可能得到-1的结果。

抱歉!评论已关闭.