(original) C # learning Note 04--Process Control 01--boolean logic 02--bitwise operator

Source: Internet
Author: User
Tags bitwise operators

4.1.2 Bitwise operators

& and | Operators also have a role in performing operations on numeric values. When used in this way, they deal with a series of bits stored in a variable, rather than variable values, so they are called bitwise operators.

Let's discuss & and | below. The & operator each bit in the first operand is compared to a bit in the same position in the second operand, and in the resulting result, the bits in each position are shown in table 4-5.

| Operators are similar, but the resulting bits are different. As shown in table 4-6.

  

The use of the ^ operator is the same as this. If there is a bit in the same position in the operand and only one is 1, the result bit is 1, as shown in table 4-7.

  

You can also use the unary operator ~ in C #, which will reverse the bits in the operand, and the result should be a bit of 1 in the operand, 0 in the result, and vice versa, as shown in table 4-8.

  

Integers are stored in the. NET is called 2 of the complement, even with the unary operator ~ will make the results look a bit odd. Assuming that the int type is a 32-bit number, the operator ~ operates on all 32 bits, which will help to see this way. For example, the full binary representation of the number 5 is:

000000000000000000000000000000101

The full binary representation of the number-5 is:

111111111111111111111111111111011

In fact, according to the 2 complement system, (-X) is defined as (~x+1). This system is very useful when adding numbers together. For example, the binary representation of adding 10 and 5 together (that is, subtracting 5 from 10) is:

000000000000000000000000000001010

+ 111111111111111111111111111111011

= 1000000000000000000000000000000101

Note: Ignoring the left-most 1, you get a binary representation of 5. A ~1=2 like this is odd because the underlying structure forces the result to be generated.

In addition to the 4 bitwise operators, this section describes the other two operators, as shown in table 4-10.

  

These operators, often referred to as displacement operators, are best illustrated with a simple example:

int var1, var2 = ten, var3 = 2;

var1 = Var2 << var3;

As a result, the value of Varl is 40. The procedure is as follows: The binary value of 10 is 1010, the value is shifted to the left by two bits, and 101000 is the 40 in decimal. In fact, a multiplication operation was performed. move each bit to the left, multiply the number by 2, so move the two-bit to the left, and multiply the original operand by 4 (2 * 2). moving one bit to the right divides the operand by 2 and discards the non-integer remainder :

int var1, var2 = 10;

var1 = var2 >> 1;

In this example, the value of VAR1 is 5, and the following code gets the value 2:

int var1, var2 = 10;

var1 = var2 >> 2;

In most code, these operators are not used, but you should be aware of the existence of such operators. They are primarily used in highly optimized code , where no other mathematical operations are used. Therefore, they are typically used for device drivers or system code.

The displacement operator also has an assignment operator, as shown in table 4-11.

  

(original) C # learning Note 04--Process Control 01--boolean logic 02--bitwise operator

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.