Friday, June 17, 2005

Another Bit Manipulation Function

count=0;//count number of bits
k=2;//number of bits to be removed from R
unsigned char a1;
num=8;//bits empty in a1


int given(unsigned char R)
{
//right now a1 has num bits empty
//hence copy min(num,8-k) bits from R to a1
b_count=min(num,8-k);
if(num<8-k)
{
a=R>>(8-num);//num bits of R left in a
a1 = a1 | a;//fill a1 and write to disk
////write a1 to disk
if(num== (8-k))
{
//we have copied entire R after removing k bits hence no fight
num=8;//since written to disk consider a1 empty
}
else //some portion of R is left and hence we must copy that into a1
{
a=R>>k;// remove ending k bits of R
a=R<<(num+k);//remove starting num bits of R

a1=a;//copy all the bits to a1
num=k+num; // k + num bits of a1 are empty as of now
}
}
else
{
a=R>>k;//remove last k bits of R
a=a< a=a>>(8-num);//align with a1
a1=a1|a;//take or of a1 and a
num=num+8-k;
}

}

No comments:

Post a Comment