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.

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子不爱跟外人说话怎么办 别人说我不说话怎么办 衣服屁股那块变亮怎么办 小孩家里说话外面不说话怎么办 2个月小宝宝便秘怎么办 3个月小宝宝便秘怎么办 微信客户不说话怎么办 一岁半宝宝便秘肛裂怎么办 网恋现在都不说话了怎么办 宝宝便秘四天了怎么办 月子里小孩吐奶怎么办 月子里的小孩吐奶怎么办 婴儿吃多了吐奶怎么办 20个月孩子便秘怎么办 一岁宝宝肛裂怎么办 婴儿吃饱了吐奶怎么办 23天新生儿吐奶怎么办 婴儿吐奶舌苔白怎么办 宝宝吐奶酸臭味怎么办? 1周岁吐奶有酸味怎么办 十多天的宝宝吐奶怎么办 未满月婴儿吐奶怎么办 2个月宝宝溢奶怎么办 四岁宝宝说话结巴怎么办 小孩说话结巴打顿怎么办 2岁宝宝突然说话结巴怎么办 2岁宝宝突然结巴怎么办 幼儿舌头起泡牙龈出血怎么办 小孩长得太快怎么办 脑出血压着神经不会说话怎么办 四岁宝宝说话有点口吃怎么办 三岁宝宝有点口吃怎么办 3岁宝宝有点口吃怎么办 三岁宝宝说话有点口吃怎么办 六岁说话重复第一个字怎么办 宝贝烧到39.5度怎么办 宝贝39度不退烧怎么办 两岁多小儿突然变得口吃怎么办 百度两周岁宝宝口吃怎么办 2岁宝宝偶尔结巴怎么办 两岁宝宝说话磕巴怎么办