using System; class OR { static void Main() { Console.WriteLine(true | false); // logical or Console.WriteLine(false | false); // logical or Console.WriteLine("0x{0:x}", 0xf8 | 0x3f); // bitwise or } }
Language Basics
'logical and' and bitwise and
using System; class BitwiseAnd { static void Main() { Console.WriteLine(true & false); // logical and Console.WriteLine(true & true); // logical and Console.WriteLine("0x{0:x}", 0xf8 & 0x3f); // bitwise and } }
control the operator evaluation sequence
using System; public class MainClass { static void Main(string[] args) { int a; a = -42; Console.WriteLine("{0}", a); int b = 2; int c = 42; a = b * (-c); Console.WriteLine("{0}", a); c = -42; a = b * (+c); Console.WriteLine("{0}", a); } }
Relational Operators
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_2___Common_Type_System
{
public class RelationalOperators
{
static void Main(string[] args)
{
int a, b;
a = 1;
b = 2;
if (a > b)
b = 10;
if (b < a)
a = 10;
if (a >= b)
b = 20;
if (b <= a) a = 20; if (a == b) b = 5; if (b != a) b = a; } } } [/csharp]
Numeric Operators 1
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */ using System; namespace Client.Chapter_2___Operators_and_Excpressions { public class NumericOperators1 { static void Main(string[] args) { int a, b, c, d, e; a = 1; a += 1; b = a; b -= 2; c = b; c *= 3; d = 4; d /= 2; e = 23; e %= 3; } } }
Numeric Operators 3
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */ using System; namespace Client.Chapter_2___Operators_and_Excpressions { public class NumericOperators2 { static void Main(string[] args) { int a,b,c,d,e,f; a = 1; //1 b = a + 1; //2 b = b - 1; //1 c = 1; d = 2; ++c; //2 --d; //1 e = --c; // e = 1 c = 1 f = c--; // f = 1 c = 0 } } }
Relational Operators 3
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_2___Operators_and_Excpressions
{
public class RelationalOperators2
{
static void Main(string[] args)
{
int a = 10, b = 20, c = 30;
if (a < 15 && b < 20) c = 10; if (a < 15 || b < 20) c = 15; if (!(a == 15)) c = 25; } } } [/csharp]