Java Reference
In-Depth Information
derbitsofanoperandyouXORwithamaskinwhichthesebitsareset.If
theremainingbitsareclearinthemask,thentheoriginalvalueofthese
The ~ operator
The~operatorinvertsallbitsofasingleoperand.Inotherwords,itcon-
vertsall1-bitsto0andall0-bitsto1.Thisactioncorrespondstotheboolean
operation.
bitwise NOT
0101 0011
operand
1010 1100
result
Figure8-6ActionoftheNOTOperator
The <, >, and >> operators
TheJavashiftleft(<)andshiftright(>and>>)operatorsareusedtomove
operandbitstotherightortotheleft.Allthreeoperatorsrequireavalue
thatspecifiesthenumberofbitstobeshifted.Thefollowingexpression
shiftsleft,by2bitpositions,allthebitsinthevariablebitPattern:
int bitPattern = 127;
bitPattern = bitPattern < 2;
10101100
OPERAND BEFORE << 1
discarded
01011000
OPERAND AFTER << 1
0shiftedin
Figure8-7Actionofthe<Operator
the most significant bit is discarded. This could spell trouble when the op-