Numeric Operators 3

image_pdfimage_print
   
 
/*
 * 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

image_pdfimage_print

/*
* 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

image_pdfimage_print

   
 
/*
C# Programming Tips &amp; 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);
        }
    }
}

           
         
     


Operator precedence

image_pdfimage_print

   
 
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example3_10.cs illustrates operator precedence
*/

public class Example3_10
{

  public static void Main()
  {

    int myInt = 2 + 5 * 10;
    System.Console.WriteLine("2 + 5 * 10 = " + myInt);

    myInt = (2 + 5) * 10;
    System.Console.WriteLine("(2 + 5) * 10 = " + myInt);

    myInt = 2 * 20 / 5;
    System.Console.WriteLine("2 * 20 / 5 = " + myInt);

  }

}


           
         
     


Prefix and postfix versions of the increment and decrement operators

image_pdfimage_print

   
 
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example3_8.cs illustrates the use of
  prefix and postfix versions of the
  increment and decrement operators
*/

public class Example3_8
{

  public static void Main()
  {

    // postfix increment
    int length = 3;
    int newLength = length++;
    System.Console.WriteLine("Postfix increment example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);

    // prefix increment
    length = 3;
    newLength = ++length;
    System.Console.WriteLine("Prefix increment example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);

    // postfix decrement
    length = 3;
    newLength = length--;
    System.Console.WriteLine("Postfix decrement example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);

    // prefix decrement
    length = 3;
    newLength = --length;
    System.Console.WriteLine("Prefix decrement example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);

  }

}

           
         
     


Illustrates the use of the shortcut operators

image_pdfimage_print

   
 
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example3_7.cs illustrates the use of
  the shortcut operators
*/

public class Example3_7
{

  public static void Main()
  {

    int length = 1;

    length += 10;
    System.Console.WriteLine("length = " + length);

    length *= 2;  // multiplies length by 2
    System.Console.WriteLine("length = " + length);

    length /= 3;  // divides length by 3
    System.Console.WriteLine("length = " + length);

  }

}

           
         
     


Illustrates the use of the bitwise operators

image_pdfimage_print

   
 
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example3_6.cs illustrates the use of
  the bitwise operators
*/

public class Example3_6
{

  public static void Main()
  {

    byte byte1 = 0x9a;  // binary 10011010, decimal 154
    byte byte2 = 0xdb;  // binary 11011011, decimal 219
    byte result;

    System.Console.WriteLine("byte1 = " + byte1);
    System.Console.WriteLine("byte2 = " + byte2);

    // bitwise AND
    result = (byte) (byte1 &amp; byte2);
    System.Console.WriteLine("byte1 &amp; byte2 = " + result);

    // bitwise OR
    result = (byte) (byte1 | byte2);
    System.Console.WriteLine("byte1 | byte2 = " + result);

    // bitwise exclusive OR
    result = (byte) (byte1 ^ byte2);
    System.Console.WriteLine("byte1 ^ byte2 = " + result);

    // bitwise NOT
    result = (byte) ~byte1;
    System.Console.WriteLine("~byte1 = " + result);
 
    // left shift
    result = (byte) (byte1 << 1);
    System.Console.WriteLine("byte1 << 1 = " + result);

    // right shift
    result = (byte) (byte1 >> 1);
    System.Console.WriteLine("byte1 >> 1 = " + result);

  }

}