Understanding Bitwise Operations in Microcontroller Programming

Previously, we discussed why microcontrollers should be programmed in C language and what programming environments are required. Today, we will introduce the basic knowledge of microcontroller programming.

First, we need to introduce bitwise operations. Bitwise operations are very common when configuring microcontroller registers. For instance, when configuring the GPIO of a microcontroller to output direction and setting it to 0, bitwise operations are very convenient.

1. Bitwise Operations in Microcontrollers

What operations are included in bitwise operations?

There are six common operations: bitwise AND “&”, bitwise OR “|”, bitwise NOT “~”, bitwise XOR “^”, left shift “<<”, and right shift “>>”. The table is as follows:

Understanding Bitwise Operations in Microcontroller Programming

Bitwise Operations Table

2. Bitwise AND “&” Logic Calculation

The logic of bitwise AND “&” is that if there is at least one 0 in the input, the output is 0; only when all inputs are 1, the output is 1. The truth table is as follows:

Input A

0

0

1

1

Input B

0

1

0

1

Output

0

0

0

1

3. Bitwise OR “|” Logic Calculation

The logic of bitwise “|” is that if there is at least one 1 in the input, the output is 1; only when all inputs are 0, the output is 0. The truth table is as follows:

Input A

0

0

1

1

Input B

0

1

0

1

Output

0

1

1

1

4. Bitwise NOT (~) Logic Calculation

Bitwise NOT (~) can be understood as NOT, meaning to flip the input; when the input is 0, the output is 1; when the input is 1, the output is 0. The truth table is as follows:

Input

0

1

Output

1

0

5. Bitwise XOR “^” Logic Calculation

Bitwise XOR (^) is used to determine whether the inputs are the same; if the two inputs are the same, the output is 0; if the inputs are different, the output is 1. The truth table is as follows:

Input A

0

0

1

1

Input B

0

1

0

1

Output

0

1

1

0

6. Left Shift “<<” and Right Shift “>>” Logic Calculation

When performing shift operations, it is important to consider whether the data is signed. In microcontroller programming, unsigned types are often used, so here we will introduce unsigned shifting.

When unsigned data is left-shifted, zeros are added on the right, and the left side is shifted out.

Understanding Bitwise Operations in Microcontroller Programming

Left Shift “<<” Illustration

When unsigned data is right-shifted, zeros are added on the left, and the right side is shifted out.

Understanding Bitwise Operations in Microcontroller Programming

Right Shift “>>” Illustration

7. The Significance of Bitwise Calculations

As mentioned earlier, bitwise logical calculations are very convenient for configuring registers because registers can be operated on a bit level. When performing operations on a specific bit of a register, it is important not to change the values of other bits.

This is just the first article; I will continue to introduce the programming implementation of bitwise operations in the future. Stay tuned!

Understanding Bitwise Operations in Microcontroller Programming

END

Source: Mastering Embedded Systems
Copyright belongs to the original author. If there is any infringement, please contact for deletion.
Recommended Reading
The Japanese Operating System that Almost Dominated the World…
Understanding the Differences Between HEX, BIN, and AXF Files in One Article
No Need for Foreign Authorization! The Milestone of Fully Independent Domestic CPU Design is Here~

→ Follow us for more updates ←

Leave a Comment

×