Tuesday, January 1, 2008

BitOperations --- What do you think SomeFunction does?

int SomeFunction(int* intArr, int numElements)
{
int bytesNeed = numElements/8 + 1;
char* buf = new char[bytesNeed];
for(int i=0; i<bytesNeed; i++)
{
buf[i] = (unsigned char)0x0;
}

for (int j=0; j<numElements; j++)
{
int byteOffset = intArr[j]/8;
int bitOffset = intArr[j]%8;
if (bitOffset == 0)
{
byteOffset = byteOffset -1;
unsigned short check = buf[byteOffset] & 0x1;
if (check == 1)
{
return 1;
}
else
{
buf[byteOffset] = buf[byteOffset] 0x1;
}
}
else
{
unsigned char check = buf[byteOffset] >> (8-bitOffset);
check = check & 0x1;
if ((int)check == 1)
{
return 1;
}
else
{
buf[byteOffset] = buf[byteOffset](0x1 << (8-bitOffset));
}

}

}
return 0;
}

No comments: