Several uncommon operators in C #

Source: Internet
Author: User
Since it wasn't long before I joined the company to develop a project with my colleagues, I didn't learn much when I got home, and I didn't know what to write? I installed Vista, but I still don't know how to use it. Today I think of some operators, but I really forget how it counts. After reading it, I feel like most of the content and examples in this article are from msdn, just to get it done later, because I have not installed the msdn online, so it is very slow and depressing.

Do not talk about addition, subtraction, multiplication, division, and so on ;&&,! =, <,>, And so on. Let's take a look at the main binary computing operators today:

~ Operators perform bitwise complement operations on the operands. The result is equivalent to reversing each bit. The bitwise complement operator is pre-defined for the int, uint, long, And ulong types. Needless to say, the example is very important. You can understand it after reading it: 1 // Cs_operator_bitwise_compl.cs
2 Using System;
3 Class Mainclass
4 {
5 Static   Void Main ()
6 {
7 Int [] Values = { 0 , Zero x 111 , 0 xfffff , Zero X 8888 , Zero X 22000022 };
8 Foreach ( Int V In Values)
9 {
10 Console. writeline ( " ~ 0x {0: X8} = 0x {1: X8} " , V, ~ V );
11 }
12 }
13 }

Output result: ~ Zero X 00000000   =   0 xffffffff
~ Zero X 00000111   =   0 xfffffeee
~ 0x000fffff   =   0xfff00000
~ Zero X 00008888   =   0xffff7777
~ Zero X 22000022   =   0 xddffffdd

& Operators can be both unary and binary operators.

The address of the operand returned by the unsafe operator ). For integer andBoolThe Type predefines the Binary & operator. For an integer, & the logic of the calculation operand is bitwise "and ". ForBoolOperand, and the logic of the calculated operand "and"; that is, if and only when both operands areTrueThe result isTrue. & The operator calculates two operators, regardless of the value of the first operand.

1 // Cs_operator_ampersand.cs
2 Using System;
3 Class Mainclass
4 {
5 Static   Void Main ()
6 {
7 Console. writeline ( True   &   False ); // Logical and
8 Console. writeline ( True   &   True ); // Logical and
9 Console. writeline ( " 0x {0: x} " , 0xf8   &   0x3f ); // Bitwise AND
10 }
11 }

 

Output result: False
True
0x38


Binary | The operators are integer and Bool Type predefined. For integer, | Calculates the bitwise "or" result of an operand. For Bool Operands, | Calculates the logical "or" result of an operand. That is, if and only when both operands are False The result is False . 1 // Cs_operator_or.cs
2 Using System;
3 Class Mainclass
4 {
5 Static   Void Main ()
6 {
7 Console. writeline ( True   |   False ); // Logical or
8 Console. writeline ( False   |   False ); // Logical or
9 Console. writeline ( " 0x {0: x} " , 0xf8   |   0x3f ); // Bitwise OR
10 }
11 }

Output result: 1 True
2 False
3 0xff

let's take a look at these three operators today. Of course they have ~ =, & =, | = Operator. Everyone knows what they mean. From the above three examples, we can see that ~ The bitwise complement is basically equal to that of less than 16 (hexadecimal). It is okay to use F minus itself to replace its own number. INT-type numbers are 32-bit in memory, that is to say, each number must be 0 before the 32-bit number, and then fill. For more functions, it can be an insecure Code pointer and memory address reference, second, it can be that when the binary value is 1 in BIT and number, the value is 1, and the other value is 0. If the logic and number are all true, the value is true, and the other value is false. For the | Operator, if one of them is 1, it is true if one is true, unless it is 0 or false. OK. With this, we know how to calculate (the application in the permission is good ).

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.