
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
126
|
Chapter 3: Classes and Structures
ColorBitMask color = ColorBitMask.YellowBitMask;
ColorBitMask newColor = color ^ ColorBitMask.RedBitMask;
00000011 == Color (yellow)
^ 00000001 == RedBitMask
00000010 == green
Use the AND (&) operator to set one or more bits to 0.
Use the OR (|) operator to set one or more bits to 1.
Use the XOR (^) operator to flip one or more bits to their opposite
values.
See Also
See Recipe 1.4; see the “C# Operators” topic in the MSDN documentation.
3.10 Making Error-Free Expressions
Problem
A complex expression in your code is returning incorrect results. For example, if you
wanted to find the average area given two circles, you might write the following
expression:
double radius1 = 2;
double radius2 = 4;
double aveArea = .5 * Math.PI * Math.Pow(radius1, 2) + Math.PI *
Math.Pow(radius2, 2);
However, the result is always incorrect.
Complex mathematical and Boolean equations in your code can easily become the
source of bugs. You need to write bug-free equations, while at the same time making
them easier to read.
Solution
The solution is quite simple: use parentheses to explicitly define the order of opera-
tions that will take place in your equation. To fix the expression presented in the
Problem section, rewrite it as follows:
double radius1 = 2;
double radius2 = 4;
double ...