Q

Emertxe-News & Blog

Home » Blog » Bit-fields: Be ‘bit’ Careful While Using!
Bit-fields: Be ‘bit’ Careful While Using!

In Embedded systems C programming, bit-fields offer great benefits in terms of optimization and flexibility for programmers. However if not used with care it can produce adverse results. In this post, let us look into signed qualifiers affecting output of the bit-field structure. Please note code snippets provided here is tested with GCC compiler [ gcc version 4.7.3 ] running under Linux environment.

 

Let us consider a simple small ‘C’ code snippet (as shown in the Fig 1 below), with a structure named bit_field, namely having three integer fields hours, minsand secshaving bit size of 5,6 and 6 respectively.

 
Bit fields in C programming
Fig 1: Definition of the bit-field structure.
 

Now let us declare a variable alarmof struct time_tand set values as 22, 12 and 20 respectively.

 
Bit fields in C programming
Fig 2: Value assignment for structure members
 

When we print these values using (simple printf statement) what could be the output? At first sight most of us will envision answers will be 22, 12 and 20 forhours, minsand secsfield. Whereas when we actually compile and run the value would be different -10, 12 and 20 (as mentioned in Fig 3 below). Now what’s wrong with this? Isn’t it obvious to print an integer value?

 
Fig 3: Actual output

 

What and where did we go wrong? Let us give some work to our grey matter!  

1. We all know that default signed qualifier for the “int” is “signed int”

2. In our case, we reserved ‘5’ bits for storing the hours field assuming we are using 24 hrs format. So, among ‘5’ bits ‘1’ bit is used for storing the sign of the number, which means only 4 bits are now available for storing actual value. In this 4 bits we can store the numbers ranging from -16 to +15 according to the formula (-2^k) to (+2^(k-1)) including “0”, where ‘k’ indicating number of bits.

3. We will see how 22 is stored in binary form in this ‘5’ bits through pictorial representation, shown in 

 

4. From the above table, it is very clear that sign bit (b4) is SET which indicates the value is negative. So, when printed using the printf statement we will get -10 (Decimal value of 10110) because of which we are getting unexpected output.

Now that we understand the problem, how do we fix it? It is very simple just qualify “int” to “unsigned int” just before the hours in the bit fields in C programming structure as shown below Fig 4 with corrected output in Fig 5.

 
Fig 4: “int” qualified to “unsigned int“
 
Fig 5: Correct output after correct usage of datatype
 

Bit wise operators definitely provide advantages, which need to be used ‘bit’ carefully. In Embedded programming environment they might lead to major issues in case of not handling it properly.  Click here to read about a issues with bit field.

YOU MAY ALSO LIKE

Emertxe’s Online Training Programs in Embedded Systems & IoT

Emertxe’s Online Training Programs in Embedded Systems & IoT

The reason why Emertxe is the best choice for career advancement: eLearning and Online training programs are the way forward in the COVID-19 disrupted world. Riding along the digital revolution will ensure engineers are future-ready with skills to not only secure but...

Our Training Programs for Freshers

Our Training Programs for Freshers

Introduction: Emertxe is the leading training institute in Bangalore for Embedded Systems and IoT domains. It is a pioneer in training freshers since 2003 by providing excellent placement opportunities for freshers. Over the years 70000+ students have made their...

Q