C# 逻辑运算符
01 /// <summary>
02 /// 2011-05-30 Geovin Du 逻辑运算符
03 /// 涂聚文 缔友计算机信息技术有限公司
04 /// </summary>
05 /// <param name="sender"></param>
06 /// <param name="e"></param>
07 protected void Page_Load(object sender, EventArgs e)
08 {
09 //1 &
10 byte oddMask = 1;
11 byte someByte = 85;
12 bool isEven;
13 isEven = (oddMask & someByte) == 0;
14 Response.Write("位与运算符:"+isEven.ToString()+"<br/>");//isEven is false
15 //2 |
16 byte option1 = 1;
17 byte option2 = 2;
18 byte totalOptions;
19 totalOptions = (byte)(option1 | option2);
20 Response.Write("位或运算符:" + totalOptions.ToString() + "<br/>");
21 //3 ^
22 byte invertMask = 255;
23 byte someinvertByte = 240;
24 byte inverse;
25 inverse = (byte)(someinvertByte ^ invertMask);
26 Response.Write("位异或运算符:" + inverse.ToString() + "<br/>");
27 Response.Write(((int)inverse).ToString() + "<br/>");
28 //4 &
29 bool inStock = false;
30 decimal price = 18.95m;
31 bool buy;
32 buy = inStock & (price < 20.00m);
33 Response.Write("布尔与运算符:" + buy.ToString() + "<br/>");
34 //5 |
35 int mileage = 2305;
36 int months = 4;
37 bool changOil;
38 changOil = mileage > 3000 | months > 3;
39 Response.Write("布尔或运算符:" + changOil.ToString() + "<br/>");
40 //6 ^
41 bool availFlag = false;
42 bool toggle = true;
43 bool available;
44 available = availFlag ^ toggle;
45 Response.Write("布尔异或运算符:" + available.ToString() + "<br/>");
46 //7. &&
47 bool insStocks = false;
48 decimal prices = 18.95m;
49 bool buys;
50 buys = insStocks && (prices < 20.00m);
51 Response.Write("条件与运算符:" + buys.ToString() + "<br/>");
52 //8. ||
53 int meleage = 4305;
54 int monthts = 4;
55 bool changeoils;
56 changeoils = meleage > 3000 || monthts > 3;
57 Response.Write("条件或运算符:" + changeoils.ToString() + "<br/>");
58 //9 副作用
59 decimal totalSpending=3692.48m;
60 bool onBudget=totalSpending>4000.00m && totalSpending<CalsAvg();//方法
61 Response.Write("副作用:" + onBudget.ToString() + "<br/>");
62 //10 is
63 int i = 0;
64 bool isTest = i is int;
65 Response.Write("is 运算符:" + isTest.ToString() + "<br/>");
66 //11 as
67 //12 sizeof
68 //13 typeof
69 //14 checked
70 //15 unchecked
71 //三元运算符
72 long democratVotes = 178888;
73 long republicanVotes = 173343;
74 string headline = democratVotes != republicanVotes ? "We Finally Have a Winner!" : recount();
75 Response.Write("三元运算符:" + headline + "<br/>");
76
77 }
78 decimal avgSpending;
79 private decimal CalsAvg()
80 {
81 return avgSpending=4002.00m;
82 }
83 /// <summary>
84 ///
85 /// </summary>
86 /// <returns></returns>
87 private string recount()
88 {
89 return "hello";
90 }