Microcontroller Embedded C Programming Lecture 99| Applicability of bitwise operators : Testing of bits

  • Post author:
  • Post category:Blog

 

Applicability of bitwise operators : Testing of bits

 

 

In this article, let’s understand about testing of bits using the bitwise AND (&) operator. We’ll understand this by doing an exercise.

 

Exercise : Testing of Bits

  • Write a program to find out whether a user entered a number is even or odd. 
  • Print an appropriate message on the console
  • Use testing of bits logic.

 

To find out whether a number is even or odd.

There are many logic you can build. But, we will use testing of bits logic.

Applicability of bitwise operators : Testing of bits
Figure 1. Testing of bits logic

 

How this works is, let’s consider a number an integer, let’s say 46. If you write 46 in binary form, you get 00101110, and here the least significant bit is 0.

For an EVEN number, the LSB is always 0.  

Let’s consider the odd number 47. 00101111 is a binary form of 47. Here the least significant bit is 1.

For the ODD number, the LSB will always be 1.   

That’s why by checking or by testing the least significant bit(LSB) of a number, we can find out whether a number is even or odd. That’s the logic. 

 

So, we understood that we wanted to test a bit. In this case, it is LSB.

How to do that?

To do that, we use a technique called Bit-Masking

 

Bit-Masking

Bit masking is a technique in programming used to test or modify the states of the bits of a given data.

Figure 2. Bit-Masking
Figure 2. Bit-Masking

 

We use Bit Masking with a Mask_value, so we have to create a Mask_value.

Here we have to decide what should be the Mask_value, and then take that Mask_value and do a bitwise AND(&) operation with a given data to get the output.

Now, let’s analyze how this works. 

Here(Figure 2) 00101110 is the input number written in a binary format. We divide this number into two areas. 0010111 I call it area1(gray color area), and 0 (lsb) is area2(pink color area), as shown in Figure 2. 

Here, in this number 0010111 (area 1) is of no interest to us because our goal is to test a bit which falls under area2. Our goal is to find out a number is EVEN or ODD. That’s why we care only about the LSB. That’s why area1 is of no interest to us, so 0 them out by using the Mask_value zeros.

So, at the output, area1 is zeroed out. That’s a reason why Mask_value is 0 for that area. So, we use a Mask_value 0 and do bitwise AND with the number.

 

But, for area1, I can’t use 0 here. If I use 0, then it doesn’t matter what is there in the area2 output will be 0. That’s why, for area2, we use the mask bit as 1, because I want to test this bit. 

That’s why to test a bit, we use 1 with the bitwise & operator. So, if this position is 0, then I get 0 here. So, the output is 0. If the output is 0, then I conclude that the number is EVEN.

So, if the number is odd, then the LSB position will be 1. 1 bitwise AND with 1 (1&1), which gives me 1. So, this output is non-zero. If the output is non-zero, I will conclude that the number is ODD. 

 

The takeaway from this article is, if you want to test a bit, use bitwise & operation. And by using this technique, you can test any bit position you want.  

For example, 00101110. I am checking the bit position 1. That’s why all these bits are 0, and I make only the second bit position as 1, as shown in Figure 3.

I consider 00101110. Here I am testing the MSP bit position, and here we are testing the first two bit positions.

Applicability of bitwise operators : Testing of bits
Figure 3. Checking bit position

 

Now, let’s try to solve our problem. Finding out whether a number is even or odd.

Applicability of bitwise operators : Testing of bits
Figure 4. EVEN or ODD using Bitwise operation

 

If( number & 1) is a logic, I would like to use it. Here the number is bitwise ANDed with a mask value which is 1.

If(number & 1) this whole expression turns out to be a non-zero value, then the expression will be true. So, in that case, the print number is odd.

If the output of if(number & 1) this expression is false, that is 0, then you can print the number as even.

 

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.