C # Bit operations explained with Example 2

Source: Internet
Author: User
Tags logical operators

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.
Operation 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. bitwise logical non-operationA 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: ~145 equals 110, the binary 01010101 is a bitwise logical non-operation, the result is equal to 10101010. In decimal notation is ~85 equals 176. 2 , bit logic and operationsBitwise 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 operationsBitwise 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 logical xor, or arithmeticA bitwise logical XOR operation makes two operands bitwise XOR. The rules for XOR operations are: 1 xor or 1 equals 0, 1 xor or 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 OperationThe 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: A 8-bit byte variable byte a=0x65 (that is, binary 01100101), moving it 3 bits to the left: the result of A<<3 is 0x27 (that is, the binary 00101000). Moves the first operand to the left by the number of digits specified by the second operand, and the vacated position is 0.
The left shift is equivalent to multiplication. The left shift is equivalent to multiply by 2, the left two is equal to 4, and the left three is the equivalent of multiply by 8.

x<<1= x*2
X<<2= x*4
x<<3= x*8
x<<4= x*16

6 , bitwise right SHIFT operationThe 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: 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). Moves the first operand to the right by the number of digits specified by the second operand, and the vacated position is 0.

The right shift is equivalent to divide. Shift right one is equivalent to dividing by 2, the right shift two is equivalent to dividing by 4, and the right shift three bits is equivalent to dividing by 8.

x>>1= X/2
X>>2= X/4
x>>3= X/8
X>>4=x/16

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 the type of the two operands is inconsistent, C # converts the type of the inconsistent type into a consistent type and then the operation. 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. -

The commonly used bit operations are mainly with (&), or (|) and non-(~), such as:

1 & 0 = 0, 1 | 0 = 1, to = 0

When designing permissions, we can convert rights management operations to C # bit operations to handle.

The first step is to create an enumeration that represents all rights management operations:

[Flags]
Publicenumpermissions{
Insert =1,
Delete =2,
Update =4,
Query =8
}

[Flags] means that the enumeration can support C # bit operations, and each of the enumerated values, we use 2 of the N-square to assign the value, so that the binary is exactly 1 = 0001, 2 = 0010, 4 = 0100, 8 = 1000, etc., each representing a permission, 1 means that the permission, 0 means No.

Next is the operation of the permission:

1. The addition of permissions, use and operation to achieve. We know, 0001 | 0100 = 0101, which means that permissions are managed with both first and third bits, and the enumeration is represented as:

    1. Permissions per = Permissions.insert | Permissions.update

2. The subtraction of permissions, using and operation + non-operation to achieve, such as the above to remove the Insert permission, the operation is:

    1. Permissions per &= ~permissions.insert is 0101 & ~0001 = 0101 & 1110 = 0100

3. Authority judgment, use and operation, when determining whether a user has the operation rights, to the user's permissions and operation permissions and operations, if the result is still operation Rights Management, it means that the user has this permission:

Permissions per = Permissions.insert |
Permissions.update;
if(per & permissionspermissions.insert = Permissions.insert)
{
//have permission to operate
}

The comparison process is 0101 & 0001 = 0001, 0001 of 0 bits are used with C # bitwise operations to set the other bits to 0, which becomes only 1 of this bit.

C # Bit operations explained with Example 2

Related Article

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.