A slightly faster option: unsigned char Data[70]; unsigned char CurrentValue; unsigned char CurrentBitIndex =3D 0; unsigned char CurrentByteIndex =3D 0; .... if( CurrentBitIndex =3D=3D 0 ) { CurrentValue =3D Data[CurrentByteIndex]; if( ++CurrentByteIndex >=3D sizeof Data ) { CurrentByteIndex =3D 0; LoadNewData(); } CurrentBitIndex =3D 8; } if( CurrentValue & 0x80 ) SendHiBit(); else SendLoBit(); CurrentValue <<=3D 1; CurrentBitIndex--; .... In 7 out of 8 rounds it will skip the first "if" block entirely, executing just one "AND", one left-shift and one decrement. Best regards, Isaac Em 12/2/2012 16:58, Isaac Marino Bavaresco escreveu: > Oops! Little mistake, forgot the "( 1 << ( BitNumber & 7 ))". > > > if( Data[ BitNumber >> 3 ] & ( 1 << ( BitNumber & 7 ))) > SendHiBit(); > else > SendLoBit(); > if( ++BitNumber >=3D sizeof Data * 8 ) > { > BitNumber =3D 0; > LoadNewData(); > } > > > Isaac > > > Em 12/2/2012 16:43, Isaac Marino Bavaresco escreveu: >> 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 =20 >>> to do efficient bit operations in C, and in this case, shift bits held = =20 >>> in multiple registers. >>> >>> Specifically, I have a stream of ~70 bytes and I need to sequentially = =20 >>> spit them out an I/O pin, on each timer interrupt (to generate a =20 >>> 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, = =20 >>> but I can't find a shift operator that let's me extract the last bit, = =20 >>> 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 =20 >>> assembly version. (I haven't actually implemented this yet, btw). Is = =20 >>> there a better way to do this in C? I don't actually need to shift if = =20 >>> there's a more efficient way to read the bit values using some type of = =20 >>> index variable. >>> >>> An incomplete thought running through my head currently is holding "1" = =20 >>> in some long var, shifting that on each iteration, then using that as = =20 >>> a mask to "AND" the Data. I'd also hold another index var (ranging =20 >>> from 0 to 2) which would tell me which data variable I was currently =20 >>> at. But this method may be even worse. >>> >>> Or is this where I do inline assembly? With inline assembly I'd =20 >>> probably expend several cycles moving the data to know register =20 >>> locations, unless there's some simple way to figure out in RAM where =20 >>> the Data2,1,0 variables are held. >>> >>> How would you C pros do it? >>> >>> Cheers, >>> -Neil. --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .