Java Reference
In-Depth Information
quentuseforthe|operatoristoselectivelysetbitsinanoperand.Theac-
tioncanbedescribedbysayingthatORingwitha1-bitalwayssetsthe
resultbit,whereasORingwitha0-bitpreservesthevalueofthecorre-
spondingbitintheotheroperand.Forexample,tomakesurethatbits5and
6ofanoperandaresetwecanORitwithamaskinwhichthesebitsare1.
Thisisshownin Figure8-4 .
0101 0101
operand
bitwise OR
1111 0000
mask
1111 0101
result
Figure8-4ActionoftheORmask
Becausebits4,5,6,and7inthemaskareset,theORoperationguaran-
teesthatthesebitswillbesetintheresultindependentlyofwhatever
valuetheyhaveinthefirstoperand.
The ^ operator
The^operatorperformstheBooleanexclusiveOR(XOR)ofthetwo
operands.Thisactionisdescribedbystatingthatabitintheresultissetif
thecorrespondingbitsintheoperandshaveoppositevalues.Ifthebits
havethesamevalue(1or0)theresultbitiszero.TheactionoftheXORop-
erationcorrespondstothetruthtableof Table8-3 .
0101 0101
operand
bitwise XOR
1111 0000
mask
1010 0101
result
Figure8-5ActionoftheXORmask
ItisinterestingtonotethatXORingavaluewithitselfalwaysgener-
atesazeroresult,sinceallbitswillnecessarilyhavethesamevalue.On
theotherhand,XORingwitha1-bitinvertsthevalueoftheotheroper-
and,because0XOR1=1and1XOR1=0(see Table8-3 ) .Byproperlyse-
lecting an XOR mask the programmer can control which bits of the
operand are inverted and which are preserved. To invert the two high-or-
 
 
Search WWH ::




Custom Search