Jump to content

How can I check if an array is equal to {0}?


nsunier
 Share

Recommended Posts

My C course is several years old and I don't remember very clearly one thing.

I've defined an array: unsigned char cur_but_solo[96]={0}; In my code, sometimes I set one value of this array to 1 or reset to 0. At the end, I want to check is this array is still equal to {0} without using a for loop. Is there a "clean" way to proceed?

Best regards,

nsunier

Link to comment
Share on other sites

No, there isn't a better way.

But you could optimize the handling by storing 8 solo button states per byte.

To set a solo flag, use

   cur_but_solo[button >> 3] |= MIOS_HLP_GetBitORMask(button);

To clear a solo flag, use

   cur_but_solo[button >> 3] &= MIOS_HLP_GetBitANDMask(button);

To check a solo flag, use

   if( cur_but_solo[button >> 3] & MIOS_HLP_GetBitORMask(button) ) { ... }  // no typo, use OR mask instead of AND

To output 8 flags to LEDs, use

   MIOS_DOUT_SRSet(sr, cur_but_solo[button >> 3]); // will copy 8 bits to a shift register

etc...

The MIOS_HLP* functions are equivalent to (1 << (button & 7)), but much faster!

Best Regards, Thorsten.

Link to comment
Share on other sites

I haven't noticed about this very useful function. Thank you! ;)

I've another question about the example header "main.h":

// status of analog toolbox application
typedef union {
  struct {
    unsigned ALL:8;
  };
  struct {
    unsigned DISPLAY_UPDATE_REQ:1;  // requests a display update
  };
} app_flags_t;

What is the function of the 8 bits named "ALL"?

Best regards,

nsunier

Link to comment
Share on other sites

Hi,

in this case the ALL is relatively useless, but in general, a Union has the advantage of reading all states at once.

So, for example if you have 8 bits that store 8 different settings, this union can be adressed as an 8bit number and easily stored and read back.

see: your favorite C-book -> Union ;)

best,

Michael

Link to comment
Share on other sites

Hi,

in this case the ALL is relatively useless, but in general, a Union has the advantage of reading all states at once.

So, for example if you have 8 bits that store 8 different settings, this union can be adressed as an 8bit number and easily stored and read back.

see: your favorite C-book -> Union ;)

best,

Michael

Hi Michael,

Ok, now I understand well.

Is it possible to define a 9bit or 67bit structure? Or is it limited to a single 8bit data?

Best regards,

nsunier

Link to comment
Share on other sites

SDCC limits it to 8 bit (unfortunately)

Initially I considered to propose a union (sometimes also called aggregate), because access to individual bits is much faster (single assembler instruction)

But for your usecase it doesn't help, because you don't access the bits directly, but via an index (button number variable) - unions cannot be accessed this way (in other words: you have to statically specify the bit number/union element which should be accessed)

Best Regards, Thorsten.

Link to comment
Share on other sites

SDCC limits it to 8 bit (unfortunately)

Not any more. I think I made a 256-bit long bitfield recently to test it out :)

NSunier I've written a big explanation of structs unions and bitfields in another thread, you should check it out! Just search for those three words in posts made by me, you'll find it.

Edit: BTW, if you missed the HLP functions, it's time to read the Function Reference again!

Link to comment
Share on other sites

Ummm, yes!

Keywords union struct bitfield by user stryd*

First hit is : Re: Bitfield s are no longer limited to 8 bits! Yay!. That's not it. That's me proving that we can now have stupid size bitfields.

Second is this thread right here.

Third is the one about bitfields: Re: Scan Matrix extended : VOIRINOV

typedef and union are actually two separate things, but TK is using them together. I'm guessing that you're looking at a bitfield. Here's an example, I'll break it down.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...