nsunier Posted May 5, 2008 Report Share Posted May 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
TK. Posted May 5, 2008 Report Share Posted May 5, 2008 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 ANDTo output 8 flags to LEDs, use MIOS_DOUT_SRSet(sr, cur_but_solo[button >> 3]); // will copy 8 bits to a shift registeretc...The MIOS_HLP* functions are equivalent to (1 << (button & 7)), but much faster!Best Regards, Thorsten. Quote Link to comment Share on other sites More sharing options...
nsunier Posted May 6, 2008 Author Report Share Posted May 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
audiocommander Posted May 6, 2008 Report Share Posted May 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
nsunier Posted May 6, 2008 Author Report Share Posted May 6, 2008 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,MichaelHi 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 Quote Link to comment Share on other sites More sharing options...
TK. Posted May 6, 2008 Report Share Posted May 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
stryd_one Posted May 7, 2008 Report Share Posted May 7, 2008 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! Quote Link to comment Share on other sites More sharing options...
nILS Posted May 7, 2008 Report Share Posted May 7, 2008 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.Ummm, no. But *tada* here it is:http://www.midibox.org/forum/index.php/topic,10725.0.html Quote Link to comment Share on other sites More sharing options...
stryd_one Posted May 7, 2008 Report Share Posted May 7, 2008 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 : VOIRINOVtypedef 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.