/// <summary>
/// 2011-05-30 Geovin Du 逻辑运算符
/// 涂聚文 缔友计算机信息技术有限公司
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
//1 &
byte oddMask = 1;
byte someByte = 85;
bool isEven;
isEven = (oddMask & someByte) == 0;
Response.Write("位与运算符:"+isEven.ToString()+"<br/>");//isEven is false
//2 |
byte option1 = 1;
byte option2 = 2;
byte totalOptions;
totalOptions = (byte)(option1 | option2);
Response.Write("位或运算符:" + totalOptions.ToString() + "<br/>");
//3 ^
byte invertMask = 255;
byte someinvertByte = 240;
byte inverse;
inverse = (byte)(someinvertByte ^ invertMask);
Response.Write("位异或运算符:" + inverse.ToString() + "<br/>");
Response.Write(((int)inverse).ToString() + "<br/>");
//4 &
bool inStock = false;
decimal price = 18.95m;
bool buy;
buy = inStock & (price < 20.00m);
Response.Write("布尔与运算符:" + buy.ToString() + "<br/>");
//5 |
int mileage = 2305;
int months = 4;
bool changOil;
changOil = mileage > 3000 | months > 3;
Response.Write("布尔或运算符:" + changOil.ToString() + "<br/>");
//6 ^
bool availFlag = false;
bool toggle = true;
bool available;
available = availFlag ^ toggle;
Response.Write("布尔异或运算符:" + available.ToString() + "<br/>");
//7. &&
bool insStocks = false;
decimal prices = 18.95m;
bool buys;
buys = insStocks && (prices < 20.00m);
Response.Write("条件与运算符:" + buys.ToString() + "<br/>");
//8. ||
int meleage = 4305;
int monthts = 4;
bool changeoils;
changeoils = meleage > 3000 || monthts > 3;
Response.Write("条件或运算符:" + changeoils.ToString() + "<br/>");
//9 副作用
decimal totalSpending=3692.48m;
bool onBudget=totalSpending>4000.00m && totalSpending<CalsAvg();//方法
Response.Write("副作用:" + onBudget.ToString() + "<br/>");
//10 is
int i = 0;
bool isTest = i is int;
Response.Write("is 运算符:" + isTest.ToString() + "<br/>");
//11 as
//12 sizeof
//13 typeof
//14 checked
//15 unchecked
//三元运算符
long democratVotes = 178888;
long republicanVotes = 173343;
string headline = democratVotes != republicanVotes ? "We Finally Have a Winner!" : recount();
Response.Write("三元运算符:" + headline + "<br/>");
}
decimal avgSpending;
private decimal CalsAvg()
{
return avgSpending=4002.00m;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private string recount()
{
return "hello";
}
C# 逻辑运算符
最新推荐文章于 2024-06-13 19:39:51 发布