Java的移位操作
来源:互联网 发布:77pepecom现在域名 编辑:程序博客网 时间:2024/06/02 21:39
摘自《Thinking in Java 4th》
原文
The shift operators also manipulate bits. They can be used solely with primitive integer types. 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 insertedat the higher-order bits. This operator
does not exist in C or C++.
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. 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.
Shifts can be combined with the equal sign (<<= or >>= or >>>=). The lvalue is replaced by
the lvalue shifted by the rvalue. 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.
- Java的移位操作
- Java的移位操作
- Java的移位操作
- Java的移位操作(转载)
- java的移位操作详解
- Java中移位的操作
- Java的移位操作(收集+纠正)
- Java的移位操作(收集+纠正+排版)
- Java的移位操作(收集+纠正)
- Java的移位操作(收集+纠正)
- Java的移位操作(收集+纠正)
- Java中的移位操作
- Java中的移位操作
- Java 移位操作
- Java 移位操作
- java移位操作
- Java 移位操作
- java之移位操作
- MLlib-Kmeans遇到的异常
- 11301
- 154. Find Minimum in Rotated Sorted Array II[hard]
- All in All POJ1936
- 11302
- Java的移位操作
- 学习别人的博客
- php高级特性-反射
- java入门3-运算符
- 文章标题
- JAXB入门教程
- 线性表的基础知识
- 线程编程练习
- 合并两个排序的链表