Csharp/CSharp Tutorial/Data Type/int Calculation

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Bitwise AND of Integer

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;

   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Enter an integer:");
           int myInt = Convert.ToInt32(Console.ReadLine());
           Console.WriteLine("Bitwise AND of Integer and 10 = {0}", myInt & 10);
       }
   }</source>

Comparing integers

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;

   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Enter an integer:");
           int myInt = Convert.ToInt32(Console.ReadLine());
           Console.WriteLine("Integer less than 10? {0}", myInt < 10);
           Console.WriteLine("Integer between 0 and 5? {0}",(0 <= myInt) && (myInt <= 5));
       }
   }</source>

Comparing the Prefix and Postfix Increment Operators

<source lang="csharp">class IncrementExample {

 public static void Main()
 {
     int x;
     x = 1;
     System.Console.WriteLine("{0}, {1}, {2}", x++, x++, x);
     System.Console.WriteLine("{0}, {1}, {2}", ++x, ++x, x);
 }

}</source>

integers and arithmetic operators

<source lang="csharp">class MainClass {

 public static void Main()
 {
   
   System.Console.WriteLine("10 / 3 = " + 10 / 3);
   System.Console.WriteLine("10 % 3 = " + 10 % 3);
   int intValue1 = 10;
   int intValue2 = 3;
   System.Console.WriteLine("intValue1 / intValue2 = " + intValue1 / intValue2);
   System.Console.WriteLine("intValue1 % intValue2 = " + intValue1 % intValue2);
 }

}</source>

10 / 3 = 3
10 % 3 = 1
intValue1 / intValue2 = 3
intValue1 % intValue2 = 1

int variable assignment

<source lang="csharp">using System; class RightAssocApp {

   static void Main(string[] args)
   {
       int a = 1;
       int b = 2;
       int c = 3;
  
       Console.WriteLine("a={0} b={1} c={2}", a, b, c);        
       a = b = c;
       Console.WriteLine("After "a=b=c": a={0} b={1} c={2}", a, b, c);
   }

}</source>

Overflowing an Integer Value

<source lang="csharp">public class Program {

 public static void Main()
 {
     // int.MaxValue equals 2147483647
     int n = int.MaxValue;
     n = n + 1 ;
     System.Console.WriteLine(n);
 }

}</source>

Using Binary Operators

<source lang="csharp">class Division {

 static void Main()
 {
     int numerator;
     int denominator;
     int quotient;
     int remainder;
     System.Console.Write("Enter the numerator: ");
     numerator = int.Parse(System.Console.ReadLine());
     System.Console.Write("Enter the denominator: ");
     denominator = int.Parse(System.Console.ReadLine());
     quotient = numerator / denominator;
     remainder = numerator % denominator;
     System.Console.WriteLine("{0} / {1} = {2} with remainder {3}",numerator, denominator, quotient, remainder);
 }

}</source>