Use bitwise operators in C Programming

Source: Internet
Author: User
Tags bitwise operators

1. cBitwise operators in languages

Because the C language is designed to replace the assembly language, it must support the computing power of the assembly language, so the C language supports all bitwise operators ). Bit operations are used to test, set, or shift the BIT (BIT) in a byte or word. In programming a microprocessor, bit operations are especially suitable for registers and I/O Ports. Therefore, this section will give a more detailed introduction to this.

The form and meaning of the six bit operators are as follows:

&: And );

|: Bitwise "or" (OR );

^: The bitwise "XOR" (XOR );

~ : "Reverse" (not );

>>: Right shift of data;

1) Bitwise "and" operations

The bitwise AND operator is used to perform the bitwise AND operation on both sides of the operator expressed in binary. This operation is the same bit in the number). The operation rule is: when both operands are 1, the output result is 1; otherwise, it is 0.

For example:

A = 0x88, B = 0x81, then the calculation result of A & B is as follows:

0x88 1000 1000 a count

& 0x81 1000 0001 B count

= 1000 0000

The & operator allows the numbers 0x88 and numbers 0x81 to have one and one, two, and two digits ...... The seven and the seven are respectively "and ". The operation rule of the "and" operation is that if one of the two operands is 0, the corresponding bit in the result is 0. In the number a and number B, only the highest bit (7th bits) is 1, so the result of this bit is 1, and all other results are 0.

Usually we can use the bitwise "and" operation as a means to close a certain (that is to say 0), for example, we want to disable the 3rd bits in number, without affecting the status of other bits, you can use a number 0xf7, that is, the binary number 1111 0111 to perform the bitwise "and" operation with the number:

0x88 1000 1000 a count

& 0xf7 1111 0111 shielding count

= 1000 0000

Note: except that the number of 3rd bits is 0, all others are 1. The operation result only sets the 3rd position in the number of A to 0, while the other bits in the number of A are not affected. That is to say, if you want to disable the nth digit Of a certain number, you only need to set the nth digit to the other digit. The other digit is 1 except that the nth digit is 0, to block others.

The preceding operation can be expressed by a = A & (0xf7) or a & = (0xf7. The functions of these two expressions are the same (see the "Composite assignment operator" section in the previous section), but the second form is often seen in the source code.

2) Bitwise "or" Operation

Bitwise "or" operator | performs bitwise "or" operations on both sides of the operator expressed in binary. This operation is the same bit in the number). The operation rule is: when both operands are 0, the output result is 0; otherwise, the result is 1.

For example:

A = 0x88, B = 0x81, then the calculation result of a | B is as follows:

0x88 1000 1000 a count

| 0x81 1000 0001 B count

= 1000 1001

Usually we can use the bitwise "and" operation as the means to set the position (I .e., the position 1). For example, we want to set the 0th bits in the number of A and the 1 position 1, without affecting the status of other bits, you can use a number 0x03, that is, the binary number 00000011 to perform the bitwise "or" operation with the number:

0x88 1000 1000 a count

| 0x03 0000 0011 shields

= 1000 1011

Note that, except for 0th and 1, all others are 0. The operation result only sets the 0th and 1 positions in, the other bits of the number a are not affected. That is to say, if the nth position of a certain number is 1, only the number and the other number need to be in the "or" phase, and the other number must be in addition to the corresponding nth position as 1, all others are 0 to block others. The preceding operation can be expressed by a = A | (0xf7) or a | = (0xf7.

3) Bitwise "XOR" Operation

The bitwise "XOR" operator ^ is used to perform the "XOR" operation on both sides of the operator using the bitwise operands expressed in binary, this calculation is based on the same bit in the number. The rule for an exclusive or operation is: if two operands are different, the corresponding output result is 1; otherwise, the result is 0.

For example:

A = 0x88, B = 0x81, then the calculation result of a ^ B is as follows:

0x88 1000 1000 a count

^ 0x81 1000 0001 shielding count

= 0000 1001

The bitwise "XOR" operation ^ has some special applications, which are described as follows:

① Bitwise "XOR" operation can reverse a specific bitwise

For example, if we want to reverse the bitwise AND highest bits of the number of A, we only need to use 0x81, that is, 10000001 of the binary number, to perform bitwise "XOR" operations with it, the calculation result is the same as above. After the operation, the value of the highest bit has changed from 1 to 0, and the value of the second bit has changed from 0 to 1, which has the effect of turning the two. The status of other bits remains unchanged.

As you can see, except for the lowest Bit and the highest bit, all others are 0. The operation result will only take the 0th and 7 digits in the number as the inverse, the other bits of the number a are not affected. That is to say, if you need to reverse the nth digit of a number, you only need to "XOR" the number and the other digit, except that the nth digit is 1, all others are 0 to block others. The preceding operation can be expressed by a = a ^ (0x81) or a ^ = (0x81.

② Directly exchange the values of two variables

For example, if the variables A = 3 and B = 4 want to exchange their values, you can perform the following operations:

A ^ = B

B ^ =

A ^ = B

First, a ^ = B:

A 0000 0011

^ B 0000 0100

A = 0000 0111

Second, B ^ =:

B 0000 0100

^ A 0000 0111

B = 0000 0011

Finally, a ^ = B:

A 0000 0111

^ B 0000 0011

A = 0000 0100

In this way, the values in the variables A and B are reversed.

4)"Inverse" Operation

Inverse Operator ~ The function is to reverse all the numbers: Set all 0 to 1, 1, and 0. For example:

1001 0110 is reversed to 0110 1001.

5) Shift Data Right

Data right shift operator> move the variables to the right several places as required. The normal form of the right shift statement is:

Variable> right-shifted digits

For example, a = 1111 0000; after a = A> 2, A = 0011 1100.

6) Move data left

Left-shift operator of data

Variable

For example, a = 1111 0000; A =

Whether it is left or right shift, when a person moves from one end, the blank space on the other end will be migrated from outside 0 (some computers send 1, for more information, see the user manual of the C compiler. This indicates that the shift is different from the loop. The bit removed from one end is not sent back to the other end. The shifted bit is lost forever. At the same time, only the corresponding digit 0 can be added to the other end.

The shift operation can be used for fast multiplication and division of integers. The left shift is equivalent to multiplication 2, while the right shift is equivalent to dividing by 2.

For example, x = 7, the Binary Expression is 0000 0111,

X

X

X

In the third left shift, one of the first shifts to the outside, while the left can only be filled with 0, so it is not equal to 112*2*2 = 448, it is equivalent to 192. When X moves back following the previous steps, it cannot return to the original value, because the loss of 1 on the Left cannot be recovered:

X> 2 0011 0000, x = 48

X> 3 0000 0110 x = 48/8 = 6

X> 1 0000 0011 x = 6/2 = 3

The shift operation can also be used with other bitwise operators to set and detect each bit of the register or data I/O interface. For details, refer to the next section.

2.Some practical methods for bit Operators

1) Learn to apply compound Operators

As described above, bitwise operators can be combined with the value assignment operator "=" to form a composite operator. Five are as follows:

>=, & =, ^ =, | =

Where, X

X> = Y, equivalent to X = x> Y;

X & = Y, equivalent to X = x & Y;

X ^ = Y, equivalent to X = x ^ y;

X | = Y, equivalent to X = x | Y;

By using composite operators in C, you can simplify the source program and optimize the target program.

2) c Some common bit operations in languages

Because the objective of learning C language is to develop the control program of the microcontroller, we pay special attention to the MPU register and the operation statement in I/O. If you want to assign values, set 0, set 1, reverse, and test some bits of porta (Port A), the following statements may be used:

① Porta = 0x87

Assign a value to the whole porta. The function is to assign the number of 1000 to Porta, that is, to the Porta's 0111, 1, 2, and 7 positions 1, and to the other positions 0.

② Porta = (1

Assign a value to the whole Porta, which is equivalent to Porta = 0x80. Assign the number 1000 to Porta, and assign the specified 0000 position 1 to the other 0. However, there are two steps:

③ Porta = (1

Assign a value to the whole Porta, which has the same role as the operation in ②, but sets the value to position 1 in position 7, 3, and 2, respectively, and sets the value to 0 for all others. It first needs to shift the values given in the three parentheses, then bitwise "and", and finally assign the values to Porta. That is:

1000 0000 (1

0000 1000 (1

| 0000 0100 (1

Porta = 1000 1100

④ Porta & = 0x80

So that the point in Porta is 0, which is equivalent to Porta = Porta & (0x80 ). Because the Binary Expression of 0x80 is 1000 0000, the highest bit is 1, and all others are 0, as a template, the bits equal to 1 (such as the 7th bits in this example) block it to keep it unchanged, and clear the other bits to 0 (whether it is 0 or 1 ). The result of bitwise "and" between Porta and 0x80 is as follows:

Porta = 0x87 1000 0111

& Amp; 0x80 1000 0000

= 1000 0000

After the operation, the original value 1 of the 7th bits is retained, and the other bits are cleared by 0. The lowest three bits are originally 1 and are now 0.

⑤ Porta & = (1

It is also equivalent to Porta & = 0x80: Here there are two steps, that is, first execute 1 in the brackets

This operation clears each bit except the specified 7th bits.

⑥ Porta & = ~ (1

This command adds an inverse symbol to the equal sign ~ . The difference with the previous operation is that before bitwise AND with Porta, we also reverse 0x80 and convert 1000 0000 to 0111 1111, then perform the bitwise "and" operation. The operation result is to clear the specified 7th bits, and the rest will remain unchanged.

7porta | = (1

Equivalent to Porta = Porta | (1

If the initial Porta value before the operation is 0x07, then:

Porta 0000 0111

| 0x80 1000 0000

Porta = 1000 0111

This operation will be at the highest position of 1, and others will remain unchanged.

Note that this command and porta = (1

3) Clever Use of bitwise operations in C Language

① Clear the specified position 1 or 0 of the Register

In practical applications, Porta | = (1

In practical applications, we often use Porta & = ~ (1

The following functions are used when the nRF905 chip is enabled to send data to the air:

Void nrf905_txsend (void)

{

Portd | = (1

Delayus (1); //> 10us

Portd & = ~ (1

}

The first digit in portd to control the trx_ce signal is set to 1, and then 0 is cleared. The output of a high and low pulse signal is a Data Transmission completed within a pulse cycle. Because the trx_ce signal is defined as pd6 bits at the beginning of the program, that is, trxce = 6, the above two lines are equivalent:

Portd | = (1

Portd & = ~ (1

② Test register refers to the positioning status

When receiving data, nRF905 sends CD, AM, and Dr Signals respectively, and MPU also checks these bits to see if they increase. If they increase, execute the next step, otherwise, the branch jumps out and the main program is returned. The following is a function for detecting these bits:

Void nrf905_rxrecv (void)

{

While (PIND & (1

While (PIND & (1

While (PIND & (1

// The data has been received by the nRF905.

Nrf905_readdata (0); // read the data in nRF905

}

Including:

While (PIND & (1

If (PIND & (1

There is an equation in brackets. We will split it and introduce its function:

1

Dr has been defined as 2 at the beginning of the Program, (1

PIND & (1

PIND is the value of the 8-bit pin of the portd port, PIND & (1

Pind xxx x1xx

& Amp; 0000 0100

Result = 0000 0100

The second digit of the result is 1, that is, the entire expression (PIND & (1

If Dr is low, there are:

Pind xxxx x0xx

& Amp; 0000 0100

Result = 0000 0000

That is, the result of the entire expression is 0, (PIND & (1

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.