C # bit operations

Source: Internet
Author: User

In C #, you can perform a bitwise logical operation on an integer operand. The meaning of the bitwise logical operation is that each bit of the operand is taken sequentially, and the logical operation of each bit is the result of each bit of the result value. The bit logical operators supported by C # are shown in table 2.9.

Count symbols

Significance

Operand type

Type of operation result

Number of objects

Instance

~

Bitwise 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

^

Bitwise logical XOR operation

2

a ^ b

<<

Bitwise LEFT Shift operation

2

A<<4

>>

Bitwise RIGHT Shift operation

2

A>>2

1, bit logical non-operation

A bitwise logical non-operation is single-purpose, with only one operand. Bitwise logical non-operation bitwise pairs the value of an operand into a non-operation, i.e. 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 is a bitwise logical non-operation, the result is equal to 01101110, in decimal notation is:

The ~145 equals 110, and the binary 01010101 is a bitwise logical non-operation, and the result is equal to 10101010. In decimal notation is ~85 equals 176.

2. Bit logic and Operation

Bitwise logic and operations perform bitwise AND operations on two operands. Rules with operations: 1 and 1 equals 1, 1 and 0 equals 0.

For example: 10010001 (binary) &11110000 equals 10010000 (binary).

3. Bit logic or arithmetic

Bitwise logic OR operation bitwise OR operation of two operands. Or the rule of operation is: 1 or 1, etc. 1, 1 or 0 equals 1,

0 or 0 equals 0. such as 10010001 (binary) | 11110000 (binary) equals 11110001 (binary).

4, bit logic XOR operation

A bitwise logical XOR operation makes two operands bitwise XOR. The rule for XOR operation is: 1 xor or 1 equals 0,

1 xor 0 equals 1, 0 xor or 0 equals 0. That is, the same 0, different 1.

For example: 10010001 (binary) ^11110000 (binary) equals 01100001 (binary).

5. Bitwise LEFT SHIFT Operation

The bitwise left shift operation shifts the entire number of digits to the left by a number of bits, left-shifted to the vacated portion 0. For example: 8-bit byte variable

Byte a=0x65 (that is, binary 01100101), shifts it to the left 3 bits: The result of A<<3 is 0x27 (that is, the binary 00101000).

6. Bitwise RIGHT SHIFT operation

The bitwise right SHIFT operation shifts the entire number of digits to the right by several bits, and the vacated portion of the right shift is 0. For example: 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 an operation is the type of the operand, if the type of the two operands is the same when the bitwise AND, OR, XOR, or operation is performed. For example, for two int variables A and B, the type of the result of the operation is the int type. If two operations

Object does not have the same type, C # converts the inconsistent type into a consistent type and then operations.

The rule of type conversion is the same as the conversion of integral type in arithmetic operation.

An expression that is connected by a bitwise operator to an integral type is a bitwise-Operation expression.

111001 1100101 1010111 A

001101 0011000 1111100 Cß is 0 o'clock, is transparent, a remains the same, otherwise the median B will prevail.

100000 1010101 1000011 B

D= C

(a| C) & (~d) | B

A: Original C:mask figure B: the map to cover

C # bit operations (GO)

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.