关于数据压缩中位运算求助!

轻飘风扬 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,晚上聊~~
...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
图灵狗 2013-03-18
  • 打赏
  • 举报
回复
整个buffer中的数据向左移位,都是基本的位运算而已。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧