Java Reference
In-Depth Information
derbitsofanoperandyouXORwithamaskinwhichthesebitsareset.If
theremainingbitsareclearinthemask,thentheoriginalvalueofthese
bitswillbepreservedintheresult,asisshownin Figure8-5 .
The ~ operator
The~operatorinvertsallbitsofasingleoperand.Inotherwords,itcon-
vertsall1-bitsto0andall0-bitsto1.Thisactioncorrespondstotheboolean
NOTfunction,asshownin Table8-3 . Figure8-5 showstheresultofaNOT
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;
Theactionofaleftshiftbya1-bitpositioncanbeseenin Figure8-7 .
10101100
OPERAND BEFORE << 1
discarded
01011000
OPERAND AFTER << 1
0shiftedin
Figure8-7Actionofthe<Operator
Theoperationoftheleft-shift,asshownin Figure8-7 , determinesthat
the most significant bit is discarded. This could spell trouble when the op-
 
 
Search WWH ::




Custom Search