Table of Contents
    Add a header to begin generating the table of contents

    Bit-Fields : Be โ€˜Bitโ€™ Careful While Using!

    In Embedded systems C programming, bit-fields offer optimization and flexibility benefits but require careful usage to avoid adverse results.

     

    The code snippet below illustrates a structure named “bit_field” with three integer fields: hours (5 bits), mins (6 bits), and secs (6 bits).

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

    Now let us declare a variable alarm of struct time_t and 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 for hoursmins and secs field

     

    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?

    Bit Fields in C programming
    Fig 3: Actual output

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

    • We all know that default signed qualifier for the โ€œintโ€ is โ€œsigned intโ€.
    • 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.
    • We will see how 22 is stored in binary form in this โ€˜5โ€™ bits through pictorial representation, shown in 
    • 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.

     

    Bit Fields in C programming
    Fig 4: โ€œintโ€ qualified to โ€œunsigned intโ€œ
    Bit Fields in C programming
    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. 

    Serial No Related Blog Posts Links
    1. The Future Scope of Embedded Systems : Trends and Predictions Click Here
    2. Online Free IoT Internship for Engineering Students | Free Certificates |2023 Click Here
    3. Emertxe Placements in Embedded Systems and Embedded IoT โ€“ 2022 (Jan-sept) Report Click Here

    People Also Ask(PAA)

    Bit fields in C programming optimize memory usage in Embedded Systems by reducing footprint and improving performance. They provide memory efficiency and compact data structures.

    • Plan data structure carefully.
    • Use appropriate data types.
    • Minimize padding.
    • Document bit field layout.
    • Consider endianness.
    • Thoroughly test and verify.

    These practices optimize memory usage and improve efficiency

    Share this material with your friend:

    Start Your
    Embedded Systems Journey Today!

    Register Now!