April 2nd Monday (四月 二日 月曜日)

本文探讨了LZ77压缩算法中的位操作函数实现细节,具体介绍了如何通过两步设置位来完成从一个字节到另一个字节的数据复制过程。首先清除不需要的位,然后设置所需的位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Today I began to read the source of LZ77.  However, I almost met a difficult at once.  There is
a function which is used to copy some bits from a byte to another byte.

  I had a look at this function.  There are two steps to set bits.  Firstly, it set all bits "1"
needed.  Secondly, it set all bits "0" needed.

void CopyBitsInAByte(BYTE* memDest, int nDestPos,
  BYTE* memSrc, int nSrcPos, int nBits)
{
 BYTE b1, b2;
 b1 = *memSrc;
 b1 <<= nSrcPos; b1 >>= 8 - nBits; // clearn bits except for needs
 b1 <<= 8 - nBits - nDestPos;  // adjust the correct position
 *memDest |= b1;  // set all 1 bits.

 b2 = 0xff; b2 <<= 8 - nDestPos;  // fill 1 into bits except for needs
 b1 |= b2;
 b2 = 0xff; b2 >>= nDestPos + nBits;
 b1 |= b2;
 *memDest &= b1;  // set all 0 bits.
}

  The above function is not complicated.  But it is not easy to understand.  Firstly, it clearn
all bits except for bits segment need copying.  From "or" operating, those bits 0 was set.  Other
bits keep its original content.  This time, the "b1" is alike "0...0XXX0..0".  At second step, the
"b1" must be set style like "1..1XXX1..1".  This time, by "and" operating all bits 1 set and other
bits are not modified. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值