Microcontroller Embedded C Programming Lecture 116| Bit Extraction

  • Post author:
  • Post category:Blog

 

Bit Extraction

 

 

In this article, let’s understand bit extraction. 

Let’s consider this problem statement. 

Extract bit positions from 9th to 14th [14:9]in a given data and save it into another variable.  

Let’s understand how we can do this. 

Figure 1. Problem statement
Figure 1. Problem statement

Let’s consider the data. Here the data is a 16-bit value, the data is 0xB410 which is written in binary like 1011010000010000.  And here we have to extract from bit positions 9 to 14. 

This can be done using the method described here.

1. Shift the identified portion to the right-hand side until it touches the least significant bit(0th bit).

That means you move the 9 to 14 portion to the extreme right until it touches the LSB.

2. And after that, mask the value to extract only 6 bits[5:0] and then save it into another variable. 

 

Let’s see how we can do the first step, which is shifting.

Bit Extraction Embedded C
Figure 2. First step

1011010000010000 is the original data, and you have to shift the 9 to 14th portion to the right. So, you have to shift 011010 this by the amount 9. If you do that, then the 9th-bit position will come and touch the 0th-bit position. 

If you right shift by the 9th time you get 0000000001011010. Here we can see that, 011010 these 6 bits came to the extreme right.

Here in this data 0000000001011010, only 011010 is the useful portion, so this is what we want to keep and 0000000001 that we have to discard. That’s why you have to zero out that portion and only keep the 011010 portions. 

For that, you have to Mask. That is the second step. 

 

2. Mask the new value to extract only 6 bits from 0 to 5 and then save it into another variable.

Bit Extraction Embedded C least significant Bit
Figure 3. Second step

Here, since we don’t want this 0000000001 [15:6] bit portion,  we have to mask that portion with zeros. We only want the 011010 [5:0] portion, so we have to mask that portion with 1’s. That’s why the mask value is 0000000000111111, that is 0x003F.

And then you have to do bitwise AND. The 6 to 15th portion will be zeroed out and we only keep the 0 to 5th portion . After shifting followed by masking the output is 0000000000011010.

 

How do you do this by writing code?

Let’s say you have the data. The data is 0xB410, and you have to save the output in the variable output.

Bit Extraction Embedded C least significant Bit
Figure 4. Code

Output = (uint8_t)((Data>>9) & 0x003F)

So, the output is equal to Data right shifted by 9, and then you have to & with 0x003F. Then, you have to save the final value into the variable output and the output is of type uint8_t, so you can also typecast uint8_t. This is how you should be doing.

That’s about bit extraction.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.