On Mon, 17 Jul 2000, Chris Eddy wrote: > Folks, I have a very long data string that I want to shift in, to the > tune of 90 or more bits. I want to shift the data in (or if possible > use a pointer to a bit with increment) in C. (You assy folks sit back > down, I know it is fairly easy). I can implement longs and use the left > shift operator, but how can I avoid carrying the one bit with a pack of > code just for that purpose? If I just do a bunch of rotates, I think C > will start with a 0 in carry for the roll operation every time. > > Any grand ideas? > > Chris Eddy The assembly version is just too easy. For 90 bits, you can do it in 12 instructions without resorting to optimization from Dmitry. The C version is harder. But here is something for bit indexing that is more generic. void set_bit(unsigned char *bit_array, char bit_position, char new_state) { if(new_state) bit_array[bit_position >> 3] |= ( 1 << (bit_position & 7) ); else bit_array[bit_position >> 3] ^= ~( 1 << (bit_position & 7) ); } I bet the compiler generates more than 12 instructions for this! but I reckon you can do it in assembly like so: set_bit: ;Get a pointer to the byte containing the bit we want: rrf bit_position,w movwf fsr rrf fsr,f rrf fsr,w andlw BIT_ARRAY_MASK addlw bit_array movwf fsr ; find 1<<(bit_position&7) [note, this could be made 2-cycles ; shorter] movlw 0101b btfsc bit_position,0 movlw 1010b movwf mask movlw 0011b btfsc bit_position,1 movlw 1100b andwf mask,f btfsc bit_position,2 swapf mask,f ; now set or clear the bit movf mask,w iorwf indf,f ; Assume we're setting. btfss new_state,0 xorwf mask,w ; assumption was wrong Scott -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics