C # character conversion, value assignment, Relational operators, logical operators, bitwise operations, and type conversion,

Source: Internet
Author: User

C # character conversion, value assignment, Relational operators, logical operators, bitwise operations, and type conversion,
I. Conversion of Characters

Corresponding ASCALL Value

0-48

A-65

A-97

Example charch = 'a ';

Console. WriteLine (int) ch); // output 97

Ii. Value assignment

// When writing code variables, you must get into the habit of defining names that conform to actual meanings, such as age, Not a or B.

Example: ①// 1 + 2 + 3 + 4 + 5

Intresult = 0;

Result + = 1; // result = result + 1;

Console. WriteLine (result );

Result + = 2; // result = result + 2;

Console. WriteLine (result );

Result + = 3; // result = result + 3;

Console. WriteLine (result );

Result + = 4; // result = result + 4;

Console. WriteLine (result );

Result + = 5; // result = result + 5;

Console. WriteLine (result );

// Multiplication

Intnum3 = 4;

Intnum4 = 5;

Num3 * = num4; // num3 = num3 * num4

Console. WriteLine (num3 );

③ // Subtraction

Note: intnum5 = 9;

Intnum6 = 5;

// Num5-= num6; // num5 = num5-num6

// Console. WriteLine (num5 );

// You can directly write it like this

Console. WriteLine (num5-= num6 );

④ // Division

Num5 = 10;

Num6 = 6;

Num5/= num6; // num5 = num5/num6;

Console. WriteLine (num5 );

⑤ // Module, etc.

Num3 = 9;

Num4 = 5;

Num3 % = num4; // num3 = num3 % num4;

Console. WriteLine (num3 );

Similar to num1 = num1 + 1; it can be written as num1 + +;

Similar to num1 = num1 + num2, you can write it as num1 + = num2;

Iii. Relational operators (comparison operators)

1,// The Value assignment has the lowest priority

Boolret = 4 = 3;

Console. WriteLine (ret );

//> = Indicates that the value is greater than or equal to either of the two.

Ret = 4> = 3;

Iv. logical operators (logic)

1. // logic and &&

// Logic or |

// The logic is not!

2. // examples, 3, and 4 are smaller than 5. It is best to write code with spaces to form a habit.

// Logic and &&

Boolret = 3 <5 & 4 <5; // symbol priority in the expression <& =

Console. WriteLine (ret); // outputs true

3 // logical or |

Ret = 3 <5 | 4 <5;

Console. WriteLine (ret );

4 // The logic is not!

Ret = true;

Ret =! Ret;

Console. WriteLine (ret );

Summary:

* Expression 1 & Expression 2: When expression 1 and expression 2 are both true, the calculation result is true. If either of the two is false

* Logic and short circuit: If expression 1 is incorrect, expression 2 is no longer executed. Therefore, you can put the first error-prone one for optimization.

* Expression 1 | expression 2: It is false when expression 1 and expression 2 are not true,

* If one of them is correct, the result is true;

* Logical or short circuit: If expression 1 is correct, expression 2 is not executed.

* Non-logical: gets the opposite value.

5. bitwise operation (digit-based)

1Console. WriteLine (6 & 11); // bitwise AND & no space, this is calculated, logic and & Space

2Console. WriteLine (6 | 11); // By bit or |

3Console. WriteLine (~ 6); // non-~ by bit ~

4// Exclusive or

Intnum1 = 6;

Intnum2 = 11;

// 13 6 11

Num1 = num1 ^ num2;

// 6 13 11

Num2 = num1 ^ num2;

// 11 13 6

Num1 = num1 ^ num2

5/*

* Bit operation summary:

* Bitwise AND: the total value of 1 is 1;

* By bit or: The value is 0 only when all values are 0;

* Bitwise non:-(number + 1); that is, add 1 to each number and then reverse it.

* Bitwise OR: the same value is 0, and the difference is 1;

* Remember to move left to right. The result of moving one digit to the left is twice the original value. <shifted to the left.

The result of moving one digit to the right is 1/2 times that of the original one.> shifted to the right.

Vi. type conversion

1,// Console. WriteLine (int. MaxValue );

// Console. WriteLine (int. MinValue );

Console. WriteLine (int. MaxValue + "" + int. MinValue); // The value range is small.

Console. WriteLine (long. MaxValue + "" + long. MinValue); // large range

2,

// Implicit type conversion, small range conversion to large range type

Intnum_int1 = 100;

Longnum_long1 = num_int1; // The int type can be put in long.

Console. WriteLine (num_long1 );

// An error will be reported and then forced conversion plus (type)

// Force type conversion

// Large scope to small scope(Because data may be lost)

Longnum_longg2 = 300;

// Force type. Be careful. data may be lost.

Intnum_int2 = (int) num_long2; // type (int) added during conversion)

Console. WriteLine (num_int2 );

Charch = 'a ';

Console. WriteLine (int) ch );

Charch1 = 'B ';

// The character is also a number, and each character corresponds to a number

Console. WriteLine (ch + response );

// Character concatenation display

Console. WriteLine ("+ ch + response); // display AB

Console. WriteLine (ch + "" + response); // You can also

Stringstr = ch + keys + "";

Console. WriteLine (str); // display 195

// Verify that the characters and numbers correspond one to one.

Console. WriteLine (int) 'A'); // display 65

Console. WriteLine (char) 65); // display

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.