首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于lzw压缩的缓冲区中存储9位代码的问题

用于lzw压缩的缓冲区中存储9位代码的问题
EN

Stack Overflow用户
提问于 2019-10-16 11:46:39
回答 1查看 141关注 0票数 0

用于我的LZW压缩代码。我选择将代码存储在9位代码中,字典大小将为512,因此只有256个新符号的空间。现在我觉得我没有为工作选择正确的缓冲区:

代码语言:javascript
复制
byte[] buffer = new byte[3];

这个缓冲区更适合存储12位,9位的等价物是什么,我如何正确地在缓冲区中存储9位?

我用它在buffer中存储8位,在buffer1中存储4位。9位的等价物是什么?

代码语言:javascript
复制
buffer[0] = (byte) (code & 255);
buffer[1] = (byte) ((code >> 8) << 4);
EN

回答 1

Stack Overflow用户

发布于 2019-10-16 13:49:38

9是一个很难处理的位数。第一个问题是:你能用8比特工作吗?

假设没有,我会考虑在字典级别进行分配,并在不考虑字节边界的情况下打包9位单词。一个512字节的字典= 4096位= 455个9位符号。你只需要一些数学运算就可以从你的码流中访问这些符号:

代码语言:javascript
复制
byte[] buffer = new byte[512];

function getWord(int wordOfs) {  
// Gets wordOfs'th 9-bit symbol from buffer, For buffer of 512 bytes, wordOfs = 0 -> 454
   if (wordOfs<0 || wordOfs>454) throw InvalidArgumentException;

   int bitsOfs = wordOfs * 9;  // Offset (in bits) of the desired 9 bit word 
   int idx = bitsOfs / 8;      // buffer[idx] contains bit 0 of the desired word
   int ofs = bitsOfs % 8;      // ... shifted this many places to the right

   // Grab at least 8 bits beyond the calculated starting point 
   unsigned word val = buffer[idx] | (buffer[idx+1]>>8);  
   // Shift and mask it down to the desired 9 bits for return
   return (val << ofs) & 0x01FF;
}

注意:我现在还不能使用Java编译器,语法可能需要改进。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58405470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档