Bit operators for C #

Source: Internet
Author: User
Tags bitwise bitwise operators

We know that any information is stored in binary form in the computer. Bitwise operators are operators that operate on data in binary order. The bit operators in the C # language are:

& and

| Or

^ XOR or

~ Take the Fill

<< Move Left

>> Move Right

Where the complement has only one operand, while the other bitwise operators have two operands. None of these operations generates an overflow. The operand of the bitwise operator is an integral type or any other type that can be converted to an integral type.

and operation

Operands are operated in binary, and the operation rules are:

0&0=0
0&1=0
1&0=0
1&1=1
This shows that except two digits are 1, and the result of the operation is 1, in any other case and the result of the operation is 0. For example, 2 and 10 are performed with the operation:

Binary representation of 2:00000010

Binary representation of 10:00001010

And operation Result: 00000010

So, the result of 2&10 is 2.

or operation

Operands are performed by bits or by operation, and the rules of operations are:

0|0=0
0|1=1
1|0=1
1|1=1
This shows that except two digits are 0, or the result of the operation is 0, in other cases or the result of the operation is 1. For example, 2 and 10 are performed or calculated:

Binary representation of 2:00000010

Binary representation of 10:00001010

Or operation result: 00001010

So, the result of 2|10 is 10.

XOR or operation

Operands are bits or operated according to the Operation rules:

0^0=0
0^1=0
1^0=0
1^1=1
This shows that when the two phases are at the same time, the difference or operation result is 0, the difference is different or the result of the operation is 1. For example, 2 and 10 perform a different or an operation:

Binary representation of 2:00000010

Binary representation of 10:00001010

XOR Result: 00001000

So, the result of 2^10 is 8.

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.