bit mover

Bharat Mediratta bharat at cf-cm.computing-maths.cardiff.ac.uk
Thu Mar 14 23:56:10 AEST 1991


In article <788 at llnl.LLNL.GOV> handeli at ocfmail.ocf.llnl.gov () writes:
>I need a C function which can move arbitrary
>bits around from one memory location to another
>with a bit offset of anything. For example,
>the call might look like
>
>movebits(a1,ioff1,a2,ioff2,nbits)
>
>where a1 and a2 are char arrays
>ioff1 and ioff2 are bit offsets into the
>char arrays
>nbits is the number of bits to move.
>a1 might be source address and a2 might be
>destination address.
>
>Anybody have such a function?


DISCLAIMER: I haven't tested this, but I think it will work.

/* 
 * Off1 & Off2 are the offsets of the bit 
 * 0 = rightmost bit.
 * No error correction
 */     
movebits(src, off1, dest, off2, nbits)
int *src, *dest;
int off1, off2, nbits;
{
        off1 = 1<<off1; 
        off2 = 1<<off2; 
        while (nbits--) 
        {
		*dest &= ~off2; /* Zero out the location */
		*dest |= ((*src & off1)? 0xFFFF : 0x0000) & off2;  /* Yuk */
                off2 <<=1; 
                off1 <<=1;
        }
}

Cheers!

 -Bharat 
--
|  Bharat Mediratta  | JANET: bharat at cm.cf.ac.uk                               |
+--------------------+ UUNET: bharat%cm.cf.ac.uk%cunyvm.cuny.edu at uunet.uucp    |
|On a clear disk...  | uk.co: bharat%cm.cf.ac.uk%cunyvm.cuny.edu%uunet.uucp at ukc|
|you can seek forever| UUCP: ...!uunet!cunym.cuny.edu!cm.cf.ac.uk!bharat       |



More information about the Comp.unix.questions mailing list