Bit Fields in C programming

Bit Fields in C Programming | Embedded Systems

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: