C # Bit operations explained and examples

Source: Internet
Author: User

A bitwise OR combination of permission strings was encountered in the project today:

First, each permission number is 2 of the n-th number

such as: k1=2; Add to

k2=4; Delete

k3=8; Modify

...

This defines the number of functional permissions that need to be bitwise OR of the number of permissions that are owned when a combination of permissions is required.

Such as:

purview = K2|K3; Assign to add and remove permissions

When you need to determine if you have a permission in a permission string, you need to do a bitwise AND.

Such as:

if ((Purview & K1) >0)//Determine if this permission string has add permission, the result >0 enters the IF statement code block

{

....

}

I'm sure there will be some doubts about it, so don't worry, I'll explain it carefully.

First, 2 of the 8-bit binary value is 00000010

4 of the 8-bit binary value is 00000100

8 of the 8-bit binary value is 00001000

Second, when the bitwise OR operation is performed on 8 and 4, the result is:

4|8 = 12

00000100 |00001000 = 00001100

Why would that be? 00000001|00000001=00000001 in the case of a bitwise OR operation; 00000001|00000000=1; 00000000|00000000=00000000

In other words, except that the 0|0 result is 0, the results of the other operations are 1.

So 00000100 |00001000 = 00001100

It says that the purview = K2|K3 Binary Value result is purview = 00000100 |00001000 = 00001100.

Thirdly, when the bitwise AND operation is performed on 8 and 4, the result is:

4&8=0

00000100 &00001000 = 00000000

Why is it so? 00000001&00000001=00000001 during the bitwise and operation; 00000001&00000000=00000000; 00000000&00000000=00000000

In other words, except that the 1&1 result is 1, the results of the other operations are 0.

So 00000100 &00001000 = 00000000

The result of the if ((Purview & K1) >0) above is 00001100&00000010 =00000000 (0>0) =false

The result of the same if ((Purview & K2) >0) is 00001100&00000100 =00000100=4=k2 (4>0) =true


In this way we understand the combination of permission strings and the principle of judging permissions, plainly speaking is the 2N of the number of times the bitwise AND and bitwise OR.

The following is a C # related syntax

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, 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, 0 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 variablebyte 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 variableByte 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 operationsobject 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. http://www.cnblogs.com/zgqys1980/archive/2010/05/31/1748404.html

C # Bit operations explained and examples

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.