I'm using an Atmega32 to read various sensors using its ADC.

Using digital logic and a few multiplexers, I've multiplexed the entirety of PORTA, using PA0:6 for address and PA7 as the input. Therefore, I can have a maximum of 128 inputs from just a single PORT.

Now, since the user will be operating the system via a computer (using RS232), it'll be possible to add or remove sensors, so the system must keep track of which addresses are free to add sensors and which addresses to read from.

I was thinking of using a 128 bit boolean array as flags to indicate if there is a sensor at a certain address.

While C doesn't have native support for single bit variables, it is possible to use bitfields to pack together up to 8 "bool variables" into a single unsigned char.

So, I've created the following struct:

typedef struct bool_s{
      uint8_t bit1:1;

Then, I created a array of type bool_t with a size of 128, hoping that everything would be nicely packed together, but sizeof() tells me that the array size is 128 bytes instead of the 16 bytes I was expecting.

Technically, I could create a single struct with 128 1 bit variables:

typedef struct Flag_s{
      uint8_t F1:1;
      uint8_t F2:1;
      uint8_t F3:1;
      uint8_t F128:1;

The problem with this approach is that while it does reduce memory usage, it's not very practical to use and it occupies way too much real estate in the code.

Is there any easy way to create a huge amount of flags or am I asking for too much? I mean, it's not like saving 112 bytes is going to make a big difference when you have 2K available, but what if I needed even more flags?

  • 1
    \$\begingroup\$ Shouldn't this be on Stack Overflow.... \$\endgroup\$
    – Sarima
    Commented Nov 10, 2015 at 14:34
  • 2
    \$\begingroup\$ @Joshpbarron meta.electronics.stackexchange.com/questions/2622/…, the writing of firmware for bare-metal or RTOS applications \$\endgroup\$
    – Bob
    Commented Nov 10, 2015 at 14:47
  • 1
    \$\begingroup\$ @Joshpbarron This is specific to embedded C, boolean variables do exist in C99 and besides, it's not like using a few extra bytes here and there is going to make any significant difference when you have GiBs of memory available, and if I said I was using microcontrollers, they'd probably tell me to go to the EE stack exchange. \$\endgroup\$
    – Chi
    Commented Nov 10, 2015 at 19:12

3 Answers 3


Bit fields don't work like that. Cventu's answer shows a correct way to use them, but in this case, I recommend avoiding them altogether.

Instead, create an array of 8-bit values and use shifting and masking to access it:

uint8_t flags[16];

//Initialize the flags
void init_flags(void)
    for (i = 0; i < 16; i++)
        flags[i] = 0x00;

//Set one flag based on the address
void set_flag(uint8_t address)
    flags[address/8] |= 0x1 << (address % 8);

//Clear one flag based on the address
void clear_flag(uint8_t address)
    flags[address/8] &= ~(0x1 << (address % 8));

//Check whether a flag is set
bool get_flag_state(uint8_t address)
    return (flags[address/8] & (0x1 << (address % 0x8))) != 0x00;

This is probably what the compiler will do with bit field accesses anyway, and it's easier to work with. Some optimizers are bad at optimizing bit fields, so the compiler might even do worse. All compilers I've seen turn division and modulus by a constant power of two into right-shift and AND instructions. You can use those operations directly if you're feeling paranoid:

flags[address>>3] |= 0x1 << (address & 0x7);

Bit fields are more like structures than arrays. In my experience, they're only useful for things that have names, like register fields.

  • 1
    \$\begingroup\$ On more advanced processors (like x86), bitfields are usually implemented using even larger elementary variables, like uint32_t. But the idea is the same: bitwise OR is used to set the flags, bitwise NOT+AND is used to clear the flags, and bitwise AND is used to check the flags, with the mask being either generated using the << operator or defined as constants with macros. \$\endgroup\$ Commented Nov 10, 2015 at 6:25
  • 2
    \$\begingroup\$ That's some expensive arithmetic (divide & mod) for low-powered micros. \$\endgroup\$
    – John U
    Commented Nov 10, 2015 at 12:53
  • 1
    \$\begingroup\$ While your code is more generic, some MCUs (e.g. PIC18) have the ability to read/set single bits in RAM directly. This would reduce the load a lot. One may check this, as compilers do not always recognize what you are doing there. \$\endgroup\$
    – sweber
    Commented Nov 10, 2015 at 14:25
  • 2
    \$\begingroup\$ @JohnU I've never seen division or modulus division by a constant power of two compile into anything other than shift and AND instructions. But you definitely don't want to divide or mod by a variable. \$\endgroup\$
    – Adam Haun
    Commented Nov 10, 2015 at 19:57
  • 4
    \$\begingroup\$ The reason bitfields don't work like that is because every element of an array has to have an address and be amenable to pointer arithmetic, which precludes an element from taking up less than the size of char (i.e. one byte). Multiple bitfields within a struct can "share" a byte, but no such luck with array elements. \$\endgroup\$
    – hobbs
    Commented Nov 10, 2015 at 20:11

It sounds a bit confusing, but maybe something like this can help:

struct bits_field {
                       unsigned char bit_7 :1;
                       unsigned char bit_6 :1;
                       unsigned char bit_5 :1;
                       unsigned char bit_4 :1;
                       unsigned char bit_3 :1;
                       unsigned char bit_2 :1;
                       unsigned char bit_1 :1;
                       unsigned char bit_0 :1;


union dt {
             struct bits_field a;
             unsigned char b;

After defining those, declare the following variable:

union dt my_data;

You will be able to write a entire group of eight bits togheter using: my_data.b

Or you can write each bit individually using: my_data.a.bit_7 (or 6,5,4,3,2,1,0)

In case that work for you, replicate this 16 times (using an array or struct) and you will be able to handle 128 bits individually.

Let us know if it worked! Good luck

  • \$\begingroup\$ I also thought about using a struct with 8 1bit variables, but then I'd have to write some sort of function to translate the address number into something I could use with such an array, which would just add confusion and overhead. Oh, and by the way, finally someone has showed me a good example of how the union data type can be useful! :P \$\endgroup\$
    – Chi
    Commented Nov 10, 2015 at 19:37
  • \$\begingroup\$ The main problem here is that bit fields may contain any number of padding bits and bytes, one of many things that make them unreliable and non-portable. A union doesn't solve that. \$\endgroup\$
    – Lundin
    Commented Nov 16, 2015 at 12:07

Create a structure where you declare eight bit flags that end up being a byte. Then create an array of these 8-bit structures.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.