Java Reference
In-Depth Information
quentuseforthe|operatoristoselectivelysetbitsinanoperand.Theac-
tioncanbedescribedbysayingthatORingwitha1-bitalwayssetsthe
resultbit,whereasORingwitha0-bitpreservesthevalueofthecorre-
spondingbitintheotheroperand.Forexample,tomakesurethatbits5and
6ofanoperandaresetwecanORitwithamaskinwhichthesebitsare1.
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-
0101 0101
operand
bitwise XOR
1111 0000
mask
1010 0101
result
Figure8-5ActionoftheXORmask
ItisinterestingtonotethatXORingavaluewithitselfalwaysgener-
atesazeroresult,sinceallbitswillnecessarilyhavethesamevalue.On
theotherhand,XORingwitha1-bitinvertsthevalueoftheotheroper-
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-