Java Reference
In-Depth Information
The & operator
The&operatorperformsabooleanANDofthetwooperands.Therulefor
theANDoperationisthatabitintheresultissetonlyifthecorresponding
bitsaresetinbothoperands.Thisactionisshownin Table8-3 .
The&operatorisfrequentlyusedtoclearoneormorebits,ortopre-
serveoneormorebitsintheoperand.Thisactionisconsistentwiththe
factthatANDingwithazero-bitclearstheresultbit,andANDingwitha
one-bitpreservestheoriginalvalueofthecorrespondingbitintheother
operand.Aspecificbitpatternusedtomanipulatebitsorbitfieldsis
sometimescalledamask.AnANDmaskcanbedescribedasafilterthat
passestheoperandbitsthatcorrespondtoa1-bitinthemask,andclears
theoperandbitsthatcorrespondto0-bits. Figure8-2 showsactionof
ANDingwithamask.
0101 1111
operand
bitwise AND
1111 0000
mask
0101 0000
result
Figure8-2ActionoftheANDMask
TheprogramnamedBit7Andlistedbelowandcontainedinthebook's
CDROM,allowstheusertoinputabyte.TheinputvalueisthenANDed
withamaskinwhichthehigh-orderbitisset.Therequiredmaskcorre-
spondstothedecimalvalue128,withthefollowingbitpattern:
10000000
WhenANDingtheuser'sinputwiththemask128wecanpredictthat
thesevenlow-orderbitsoftheresultarezero.RecallthatANDingwitha
zerobitalwaysproduceszero.Also,thatthevalueofthehigh-orderbitof
theresultwillbethesameasthecorrespondingbitintheuser'sinput.
In Figure8-3 , thehigh-orderbitoftheresultcanbeeitherthevalue0
or 1. Since the seven low-order bits are always zero, you can conclude
that the result will be non-zero if bit 7 of the operand was 1. If the result is
zero, then bit 7 of the operand was zero.
 
 
Search WWH ::




Custom Search