Java Reference
In-Depth Information
erandisasignednumber,sinceinsignedrepresentationsthehigh-order
bitencodesthesignofthenumber.Therefore,discardingthehigh-order
bitcanchangethesignofthevalue.ThiswouldbetrueforallJavainte-
gerdatatypes,exceptchar,whichisunsigned.Intheexamplein
Figure
positivevalue.Youmusttakethisintoaccountwhenleft-shiftingsigned
quantities.
Therearetworight-shiftoperatorsinJava,>and>>.Thesimple
right-shiftoperator(>)shiftstheleftoperandbythenumberofbitscon-
tainedintherightoperand,forexample:
int bitPattern = 127;
bitPattern = bitPattern > 1;
Intherightshift,thelow-orderbitisdiscardedandthehigh-orderbitis
duplicatedintoallthebitsthatwereabandonedbytheshift.Theresultis
extendingthesignbitintothenewoperand.Theactionofarightshiftby
10101100
OPERAND BEFORE >> 1
HOB
reproduced
discarded
11010110
OPERAND AFTER >> 1
Figure8-8Actionofthe>Operator
Theunsignedrightshiftoperator(>>)issimilartotherightshift(>),
exceptthatinthe>>thevacatedpositionsontheleftoftheoperandare
filledwithzeros.
Figure8-9
showsanunsignedrightshiftby1bitposi-
tion.
10101100
OPERAND BEFORE >>> 1
0shiftedin
discarded
01010110
OPERAND AFTER >>> 1
Figure 8-9 Action of the >> Operator