C # Learning -- Introduction (First Day ),

Source: Internet
Author: User
Tags bitwise operators

C # Learning -- Introduction (First Day ),
I. Console output

Output in the console:

1 console. writeline ();

2 console. readkey ();

The last sentence is to ensure that the pop-up window will not flash.

Ii. Notes

1. Single line comment:

1 // int a = 90;

Shortcut: Ctrl + K + c

2. Multi-line comment

2/* int a = 12; float B = 10.2 ;*/

Shortcut Key :/**/

Iii. Data Types

1. Value Type

  • Datetime Event Type
  • Enum Enumeration type
  • Struct Structure Type
  • Int, float, and double numeric types

2. Reference Type

  • Char character type (default value: '', for example, char a = 'you ')
  • Class type (the type defined by the user through class)

3. Other Types

(1) Integer

  • Int int32
  • Short (short integer) int16
  • Long (long integer) int64

(2) Time-type Application Instances

1 // time type, obtain the current time () 2 DateTime p = DateTime. Now; 3 // obtain the international time 4 DateTime k = DateTime. UtcNow;

 

4. Data Type Conversion

(1) implicit conversion

1 int num=100;2 float num1=num;

Routine:

  • Can only be converted from low-precision type to high-precision type;
  • The two types of data to be converted must be consistent;

(2) display type conversion

Method 1: Convert using the Parse Method

1 string str=“45”;2 int num=int.parse(str);3 int strnum=12;4 float num2=float.parse(strnum);

Method 2: Convert using the Convert method

1 string str=“21.52”;2 double dou=convert.todouble(str);3 int num=convert.toint(str);

Routine:

  • Only high-precision type can be converted to low-precision type (some data may be lost, such as decimal part)
Iv. Variables

1. Scope

1             int b = 3;2             int c = 7;3             if (b > 0)4             {5                int k = 100;6                Console.WriteLine("k");7             }                      

(1) local variables

The above k is a local variable. Scope: from the beginning of braces to the end of braces. To use it outside braces, you need to define the variable again.

(2) global variables

The above B and c are global variables, and the scope is within the main function, which can be used anywhere without further definition.

(3) static modified Variables

Variables modified with static (that is, global variables) have no function limitations and can be used anywhere, but must be written at the top, such:

5. logical operators (bitwise operators)

Bitwise operators are operators that perform operations on data in binary bits. Bitwise operations are supported by many other languages, such as C, C ++, and Java. Note that bit operations support basic data types, such as byte, short, char, int, and long. C # supports the following bit operations:

  • Bitwise AND &
  • By bit or |
  • Bitwise inversion ~
  • Shift left <
  • Shift right>
  • Exclusive or ^

In C #, the median operation is no different from the bitwise operation in C, and the bitwise operation speed is relatively fast. If you are skilled, the processing is relatively convenient, especially in some permissions and other related settings, such: when you use 1, 2, 4, 8, 16, 32, and 64 to view, add, edit, modify, delete, and approve permissions, if a user's final permission is a combination of multiple permission values, it is quite convenient to use bitwise operations to determine whether a user has certain permissions.

Example 1:

1 using System; 2/* 4 * Description: This program describes how to perform bit operations in C. 6 **/7 public class BitAction 8 {9 public static void Main (string [] args) 10 {11 int [] power = new int [] {1, 2, 4, 8, 16, 32, 64}; 12 int value = 126; 13/* 14*1 binary form: 0000000115*2 binary form: 0000001016*4 binary form: 0000010017*8 binary: 0000100018*16 binary: 0001000019*32 binary: 0010000020*64 binary: 0100000021*126 binary: 0111111022 */23 for (int I = 0; I <power. length; I ++) 24 {25 if (valu E & power [I])! = 0) 26 {27 Console. writeLine ("permission represented by power [{0}] = {1}", I, power [I]); 28} 29} 30 Console. writeLine ("bitwise AND: 126 & 4 = {0}", value & 4); 31 Console. writeLine ("by bit or: 126 | 4 = {0}", value | 4); 32 Console. writeLine ("shift left: 126 <4 = {0}", value <4); 33 Console. writeLine ("right shift: 126> 4 = {0}", value & 4); 34 Console. writeLine ("unusual or: 126 ^ 4 = {0}", value ^ 4); 35 Console. writeLine ("bitwise inversion :~ 126 = {0 }",~ Value); 36 Console. ReadLine (); 37} 38}

The result is as follows:

Example 2:

1 // Example 2 using System; 3 class MikeCat 4 {5 public static void Main () 6 {7 int a = 6 & 3; 8 Console. writeLine ("a = {0}", a); 9 // The binary value of 6 is 00000011, 3 is 00000010, And the bitwise value is equal to, that is, 2. 10 11 int B = 6 | 3; 12 Console. writeLine ("B = {0}", B); 13 // The binary value of 6 is 00000011, And the binary value of 3 is 00000111, which is equal to 714 by bit or after, that is, 15 int c = ~ 6; 16 Console. writeLine ("c = {0}", c); 17 // The binary value of 6 is 00000110, And the bitwise inversion value is 11111001, that is,-718 19 int d = 6 ^ 3; 20 Console. writeLine ("d = {0}", d); the binary value of 21 // 6 is 00000011. The binary value of 3 is 00000101, which is equal to or greater than by bit, that is 522 23 int e = 6 <3; 24 Console. writeLine ("e = {0}", e); 25 // The binary value of 6 is 00000110, and the value after the three digits left is 00101000, that is, 4826 27 int f = 6> 2; 28 Console. writeLine ("f = {0}", f); 29 // The binary value of 6 is 00000110, And the right shift is equal to 00000001, that is, 130} 31}

 

 

Reference URL by bit: http://www.cnblogs.com/yiki/archive/2008/03/05/1091378.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.