You can use a data type that hold all your bits, for example unsigned long and then just: // check least significant bit and act appropriately if ( 1 =3D=3D mybits & 1 ) { // raise output high } else { // put output low } // do the shit for all 32 bits mybits >>=3D 1; Tamas On 12 February 2012 14:03, PICdude wrote: > 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 int main() { char *a,*s,*q; printf(s=3D"int main() { char *a,*s,*q; printf(s=3D%s%s%s, q=3D%s%s%s%s,s,q,q,a=3D%s%s%s%s,q,q,q,a,a,q); }", q=3D"\"",s,q,q,a=3D"\\",q,q,q,a,a,q); } --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .