**Applicability of bitwise shift operators**

**Applicability**** of bitwise shift operations**

- Bitwise shift operations offer a powerful toolset for manipulating data bits, particularly when combined with other bitwise operators.
- These operations find particular utility in bit masking and manipulation scenarios, with a primary focus on bit setting and clearing.

**Setting of Bits**

Let’s delve into the process of setting specific bits using bitwise shift operators.

Consider the task of setting the 4th bit in a given data value.

There are 2 methods.

**Method 1:**

As illustrated in Figure 1, a straightforward approach involves defining a mask value.

This is the first method( as shown in Figure 1) that we used in the previous posts.

In this case, 0x08 is the given data.

To set the 4th bit, you have to find out a mask value. 00010000 is the mask value here and you have to do bitwise OR(|). And the mask value here happens to be 0x10.

**Code:**** Data = Data | 0x10.**

So, if you do that you get the result. The output is 00011000. It turns out to be 0x18.

In the output, you can see that the 4th bit is indeed set.

Now the problem with this method is you have to create a mask value. For this exercise creating a mask value is very much easier because here the width of the data is just 8 bits. So, in the previous example when we were coding for our led_on exercise, you just saw that creating a mask value for 32 bits data or 64 bits data is a tedious process.

So, that’s why there is one shortcut method you can follow.

**Method 2:**

A more efficient technique, depicted in Figure 2, employs bitwise left shift operators.

I would recommend you to use method 2 instead of method 1.

Here, what you do is, take the number 1. 00000001 is a binary value of the number 1. And then you left shift this 1 by the amount given in the problem. In the problem, they have told me to set the 4th bit. That’s why I shift the number 1 four times to the left.

What happens?

After shifting the number 1 to four times to the left you get 00010000. So, 1 appears in the fourth-bit position. Just check the mask value in method 1, so both are the same. So, that’s how you quickly find out the mask value.

**Code:**** Data = Data | (1<<4)**

(1<<4) this entity will finally get resolved into the value 00010000. So, that’s why you need not calculate the mask value, you use the left shift operator to shift the value 1 to the left.

**Clearing of bits**

Now let’s explore a clearing using shift operators.

The problem is the clear 4th bit of given data.

**Method 1**

00011000 is a given data, that is 0x18.

Here, in the traditional method if you want to clear the 4th bit, then you have to first create the mask value. So in the mask value, the bit position corresponds to the 4th bit we keep 0, and the rest all we keep as 1. You have to calculate it. The mask value happens to be 0xEF, which is 11101111.

**Code:**** Data & 0xEF**

Then you do bitwise AND. So, that will give you the output is 00001000, where the fourth bit is cleared, as shown in Figure 3.

**Method 2**

Now instead of doing method 1, you can do method 2(as shown in Figure 4).

So, take the number 1, and let’s left shift four times because we want to clear the 4th bit. After shifting the number 1 to four times to the left you get 00010000.

And after that, you negate that value.

When you negate that value, what happens?

zeroes become ones. So, you get 11101111. 0 appears in the fourth-bit position.

Just check the mask value in method 1, so both are the same. So, that’s how you quickly find out the mask value.

**Code:**** Data = Data & ~(1 << 4)**

In this method, you negate the value.

**Advantages of Method 2:**

The second method, which involves bitwise shift operators, stands out as the preferred choice due to its simplicity and elegance. This approach sidesteps the need for calculating mask values manually, reducing the likelihood of errors and streamlining the programming process. The technique’s efficiency becomes more apparent in complex scenarios, where managing mask values becomes unwieldy.

There is no question of creating this mask by calculating the bit positions. So, now you can appreciate this method. This is very easy and I would recommend you to use this method in programming.

Now by using these techniques, let’s re-attend our led_on exercise. Instead of using mask values like these(method 1), we can use shift operators(method 2) and we will fix our code in the following article.

**FastBit Embedded Brain Academy Courses**

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