Java Reference
In-Depth Information
erandisasignednumber,sinceinsignedrepresentationsthehigh-order
bitencodesthesignofthenumber.Therefore,discardingthehigh-order
bitcanchangethesignofthevalue.ThiswouldbetrueforallJavainte-
gerdatatypes,exceptchar,whichisunsigned.Intheexamplein Figure
8-7 , theoriginalnumber,whichisnegative(high-bitset),ischangedintoa
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
1bitpositionisshownin Figure8-8 .
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
 
 
Search WWH ::




Custom Search