C # bitwise operator Collation _c# Tutorial

Source: Internet
Author: User
Tags bitwise
The bit logic operators supported by C # are shown in table 2.9.
Operational symbols
Significance
Operation Object Type
Operation result Type
Number of objects
Instance
~
Bit logical non- operation
Integral type, character type
Integral type
1
~a
&
Bit logic and operations
2
A & B
|
Bit logic or operations
2
A | B
^
Bit logical XOR or operation
2
a ^ b
<<
Bit left shift operation
2
A<<4
>>
Bitwise RIGHT Shift operation
2
A>>2
1, bit logical non-operation
Bit logical non-operation is a single purpose, only one Operation object. Bitwise logical operations do not operate on the value of an Operation object by bitwise, that is, if one is equal to 0, it is converted to 1, and if one is equal to 1, it is converted to 0.
For example, the binary 10010001 bitwise logic is not operational, the result is 01101110, in decimal notation is:
The ~145 equals 110, and the binary 01010101 is bitwise-logical, and the result equals 10101010. In decimal notation is ~85 equals 176.
2 , bit logic and Operation
The bitwise logic and operation of the two operations object bitwise AND operation. With the operation of the rule: 1 and 1 equals 1, 1 and 0 equals 0.
For example: 10010001 (binary) &11110000 equals 10010000 (binary).
3, bit logic or operations
Bitwise logic or an operation to bitwise or operate on two operations objects. Or the rule of operation is: 1 or 1, etc. 1, 1 or 0 equals 1,
0 or 0 equals 0. Like 10010001 (binary) | 11110000 (binary) equals 11110001 (binary).
4 , bitwise logical XOR, or Operation
A bitwise logical XOR OR operation converts two operations objects to or from a bitwise. The rule of XOR or operation is: 1 xor or 1 equals 0,
1 xor or 0 equals 1, 0 is different or 0 is equal to 0. That is: the same 0, the difference is 1.
For example: 10010001 (binary) ^11110000 (binary) equals 01100001 (binary).
5 , bit left shift operation
The left shift operation shifts the entire number of digits to the left, and the vacated portion of the left 0. For example: A 8-bit byte variable
Byte a=0x65 (that is, binary 01100101) moves it left 3 bits: The result of A<<3 is 0x27 (that is, binary 00101000).
6 , bitwise right SHIFT operation
The bitwise right SHIFT operation shifts the entire number of digits to the right, and the vacated portion of the right is filled with 0. For example: A 8-bit byte variable
Byte a=0x65 (both (binary 01100101)) shifts it to the right 3 bits: The result of A>>3 is 0x0c (binary 00001100).
The type of the result of the operation is the type of the Operation object, if the type of the two operands is the same for the bitwise, OR, XOR, or operation. For example, two int variables A and B are done with operations, the type of the result of the operation is int type. If two operations
Object, C # will convert the inconsistent type to a consistent type and then perform the operation.
The rules for type conversions coincide with the conversion of integral types in arithmetic operations.
An expression that is connected by a bitwise operator to an integral type is a bitwise operation expression.

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.