In the first message I made a mistake, forgot one left-shift. See my reply to my own message. Besides, afterwards I posted a second solution that may be slightly more efficient. Isaac Em 14/2/2012 02:59, PICdude escreveu: > I goofed on the original question... I have 70 bits, so 9 variables =20 > should be enough. But I see what you're doing here and it looks nice. =20 > Will try it out. > > Cheers, > -Neil > > > > Quoting Isaac Marino Bavaresco : > >> Did I understand it right and you shift a chain of 70 data registers and >> test the lowest bit every round? >> >> We discussed many months ago a more efficient way of testing arbitrary >> bits in an array: >> >> >> char Data[70]; >> int BitNumber =3D 0; >> >> ... >> >> if( Data[ BitNumber >> 3 ] & ( BitNumber & 7 )) >> SendHiBit(); >> else >> SendLoBit(); >> if( ++BitNumber >=3D sizeof Data * 8 ) >> { >> BitNumber =3D 0; >> LoadNewData(); >> } >> >> ... >> >> >> No real shifting of the data is done, so the size of the array doesn't >> matter for the efficiency of the process. >> This routine should be much faster than shifting a chain of 70 bytes. >> >> >> Best regards, >> >> Isaac >> >> >> >> >> >> Em 12/2/2012 12:03, PICdude escreveu: >>> Ah yes, I knew this time would come, where I'd need an efficient way >>> to do efficient bit operations in C, and in this case, shift bits held >>> in multiple registers. >>> >>> Specifically, I have a stream of ~70 bytes and I need to sequentially >>> spit them out an I/O pin, on each timer interrupt (to generate a >>> specific output data stream). >>> >>> In assembly, I'd hold these in several registers and do something like.= ... >>> >>> rrf Data9,F >>> rrf Data8,F >>> ... >>> rrf Data0,F >>> btfsc STATUS,C ; C hold current bit value >>> goto SendHiBit >>> SendLoBit: >>> ... >>> >>> >>> In C though, it's nice to have single variables that can hold 4 bytes, >>> but I can't find a shift operator that let's me extract the last bit, >>> so I'm thinking I'd have to so something like ... >>> >>> OutBit =3D Data0 & 0b1; // Extract lowest bit and hold >>> Data0 >> 1; // Assume long (4 bytes) >>> if (Data1 & 0b1) >>> Data0 &=3D 0b1; // Uppermost bit =3D 1 >>> Data1 >> 1; >>> if (Data1 & 0b1) >>> Data1 &=3D 0b1; >>> Data1 >> 2; >>> // Send output bit here... >>> >>> >>> As compact as it looks, it seems so inefficient compared to the >>> assembly version. (I haven't actually implemented this yet, btw). Is >>> there a better way to do this in C? I don't actually need to shift if >>> there's a more efficient way to read the bit values using some type of >>> index variable. >>> >>> An incomplete thought running through my head currently is holding "1" >>> in some long var, shifting that on each iteration, then using that as >>> a mask to "AND" the Data. I'd also hold another index var (ranging >>> from 0 to 2) which would tell me which data variable I was currently >>> at. But this method may be even worse. >>> >>> Or is this where I do inline assembly? With inline assembly I'd >>> probably expend several cycles moving the data to know register >>> locations, unless there's some simple way to figure out in RAM where >>> the Data2,1,0 variables are held. >>> >>> How would you C pros do it? >>> >>> Cheers, >>> -Neil. >> >> -- >> http://www.piclist.com PIC/SX FAQ & list archive >> View/change your membership options at >> http://mailman.mit.edu/mailman/listinfo/piclist >> > > --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .