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 } }
Month: February 2011
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]
Demonstrates compound assignment operators
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Assign.cs - Demonstrates compound assignment operators // // Compile this program with the following command line: // C:>csc Assign.cs // namespace nsAssignment { using System; public class Assign { static public void Main () { unsafe { int x = sizeof (decimal); Console.WriteLine ("sizeof decimial = " + x); } // // Start with an integer variable int Var = 2; // // Show the starting value Console.WriteLine ("At the beginning, Var = {0}", Var); // // Multiply the variable by something Var *= 12; Console.WriteLine ("After Var *= 12, Var = {0}", Var); // // Add something to the variable Var += 42; Console.WriteLine ("After Var += 42, Var = {0}", Var); // // Divide the variable by something Var /= 6; Console.WriteLine ("After Var /= 6, Var = {0}", Var); // // Shift the bits in the variable four spaces to the left // This is the same as multiplying by 16 (2 to the fourth power) Var <<= 4; Console.WriteLine ("After Var <<= 4, Var = {0}", Var); // // Shift the bits in the variable four spaces to the right using // and expression on the right. This is the same as dividing // by 16. int Shift = 3; Var >>= Shift + 1; Console.WriteLine ("After Var >>= Shift + 1, Var = {0}", Var); // // Modulo divide the variable by something Var %= 6; Console.WriteLine ("After Var %= 6, Var = {0}", Var); } } }