C # bit operations and Examples

Source: Internet
Author: User

In C #, you can perform logical operations on integer computing objects by bit. The meaning of logical operations by bit is that each bit of the object to be operated is obtained in sequence for logical operations. The logical operation result of each bit is each bit of the result value. C # The supported bitwise logical operators are shown in Table 2.9.

Operator number Meaning Calculation object type Calculation Result type Number of Objects Instance
~ Bit LogicNonOperation Integer type Integer 1 ~ A
& Bit LogicAndOperation 2 A & B
| Bit LogicOrOperation 2 A | B
^ Bit LogicExclusive orOperation 2 A ^ B
< BitMove leftOperation 2 A <4
> BitRight ShiftOperation 2 A> 2
  1, Bit logical non-operationBit logical non-operation is a single purpose and only one operation object. Bitwise logical non-operation performs non-operation on the value of the computing object by bitwise. That is, if a bit is equal to 0, it is converted to 1. If a bit is equal to 1, convert it to 0. For example, if the bitwise logic is not performed on Binary 10010001, the result is equal to 01101110, which is represented in decimal :~ 145 is equal to 110; bitwise logic is not performed on Binary 01010101, and the result is equal to 10101010. In decimal format ~ 85 equals 176. 2 Bit logic and OperationBitwise logic and Operations perform and perform operations on two calculation objects by bit. And calculation rules: 1 and 1 are equal to and 0 is equal to 0. For example, 10010001 (Binary) & 11110000 equals 10010000 (Binary ). 3, Bit logic or operationBitwise logic or operation performs or on two operation objects by bit. The rule of the or operation is: 1, 1, or 0 equals 1, 0 or 0 equals 0. For example, 10010001 (Binary) | 11110000 (Binary) equals 11110001 (Binary ). 4 Bitwise logical exclusive or operationBitwise logical exclusive or operation performs bitwise exclusive or operation on Two Computing objects. The rule for an exclusive or operation is: 1 exclusive or 1 equals 0, 0 exclusive or 0 equals 0. That is, 0 is the same, and 1 is different. For example, 10010001 (Binary) ^ 11110000 (Binary) equals 01100001 (Binary ). 5 Bitwise left shift operationThe bitwise left shift operation shifts the entire number to several places by bit, and the portion left after the shift is left is 0. For example, if the eight-bit BYTE Variable byte a = 0x65 (that is, 01100101 of the binary value), remove it three places to the left: the result of a <3 is 0x27 (00101000 of binary ). 6 Bitwise right shift operationThe bitwise right shift operation shifts the entire number to several places by bit, and the portion left blank after the right shift is set to 0. For example, the eight-bit BYTE Variable byte a = 0x65 (both (Binary 01100101) shifts it three places to the right: a> 3 is 0x0c (Binary 00001100 ). When bitwise AND, Or, exclusive or operations are performed, if the types of the two operation objects are the same, the type of the operation result is the type of the operation object. For example, if two int variables A and B are compared, the type of the operation result is still Int. If the types of the two calculation objects are inconsistent, C # converts the types of the inconsistent objects to the same type and then performs the operation. The type conversion rules are the same as those for integer conversions in arithmetic operations. A bitwise operator is a bitwise expression that connects integer values. -
From: http://blog.csdn.net/icyleaf1026/archive/2007/08/25/1758243.aspx

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.