关于数据压缩中位运算求助!
轻飘风扬 2013-03-18 12:00:56 在阅读过程中中有下面的
***********************************************************************************
bit_rot_left()函数说明:
void bit_rot_left(unsigned char *bits, int size, int count );
Return Value None.
Description Rotates the buffer bits, containing size bits, to the left count
bits. After the operation, the leftmostcount bits become the count rightmost bits
in the buffer, and all other bits are shifted accordingly.
Complexity O (n β), where n is the number of bits rotated to the left and β is
the number of bits in the buffer.
函数接口实现 C语言
void bit_rot_left(unsigned char *bits, int size, int count) {
int fbit,
lbit,
i,
j;
/*****************************************************************************
* *
* Rotate the buffer to the left the specified number of bits. *
* *
*****************************************************************************/
if (size > 0) {
for (j = 0; j < count; j++) {
for (i = 0; i <= ((size - 1) / 8); i++) {
/********************************************************************
* *
* Get the bit about to be shifted off the current byte. *
* *
********************************************************************/
lbit = bit_get(&bits[i], 0);
if (i == 0) {
/*****************************************************************
* *
* Save the bit shifted off the first byte for later. *
* *
*****************************************************************/
fbit = lbit;
}
else {
/*****************************************************************
* *
* Set the rightmost bit of the previous byte to the leftmost *
* bit about to be shifted off the current byte. *
* *
*****************************************************************/
bit_set(&bits[i - 1], 7, lbit);
}
/********************************************************************
* *
* Shift the current byte to the left. *
* *
********************************************************************/
bits[i] = bits[i] << 1;
}
/***********************************************************************
* *
* Set the rightmost bit of the buffer to the bit shifted off the *
* first byte. *
* *
***********************************************************************/
bit_set(bits, size - 1, fbit);
}
}
return;
}
int bit_get(const unsigned char *bits, int pos) {
unsigned char mask;
int i;
/*****************************************************************************
* *
* Set a mask for the bit to get. *
* *
*****************************************************************************/
mask = 0x80;
for (i = 0; i < (pos % 8); i++)
mask = mask >> 1;
/*****************************************************************************
* *
* Get the bit. *
* *
*****************************************************************************/
return (((mask & bits[(int)(pos / 8)]) == mask) ? 1 : 0);
}
void bit_set(unsigned char *bits, int pos, int state) {
unsigned char mask;
int i;
/*****************************************************************************
* *
* Set a mask for the bit to set. *
* *
*****************************************************************************/
mask = 0x80;
for (i = 0; i < (pos % 8); i++)
mask = mask >> 1;
/*****************************************************************************
* *
* Set the bit. *
* *
*****************************************************************************/
if (state)
bits[pos / 8] = bits[pos / 8] | mask;
else
bits[pos / 8] = bits[pos / 8] & (~mask);
return;
}
问题是这个代码实在是理解不了~求讲解,或者留下QQ,晚上聊~~