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

轻飘风扬 轻飘风扬 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在阅读过程中中有下面的
***********************************************************************************
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,晚上聊~~
...全文
评论 1
收藏 已收藏
分享
返回顶部
轻飘风扬
C语言 70,039 成员 243,237 内容