我试图编写一个可以读写12位的小程序。输入不应该有任何问题,但我会包含它,以便您更好地理解问题。输入应该由下面包含的OFStream12Bits.cpp / main.cpp创建为sample.lzw,输出应该从write函数读取sample.lzw。我在读取代码时遇到输出问题并且主要代码不匹配。我认为问题来自运营商>>并且readBit函数确实不确定。 非常感谢您的帮助,我已经坚持了一段时间!
readbit的说明如下......
//basic readBit
//read12Bits(): 12Bit =
//declare Result : 12Bit = 0;
//for i = 1 to 12
//do
//declare lBit : Bit = get bit from input
//if(lBit == 1)
//then Result = (1 << (i-1)) + Result; //set bit at index i
//od
//return result
我不明白的部分是我需要返回*这个但是没有+运算符所以我不能使用结果来设置索引i的位。目前我有这样的代码。
IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
//int Result = a12BitValue;
//a12BitValue = ((a12BitValue & 0x0fff) << 1);
a12BitValue = a12BitValue & 0x0fff;
for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}
readBit的说明如下......
//implements mapping process. returns 0 or 1 depending on value of fBuffer[fByteIndex] & (1 << (fBitIndex - 1))
//see how it works with experiments
//at start check if (fByteCount == 0){reload();} then use reload() called as buffer does not contain any data before calling reload
//next fetch the bit store and then advance fByteIndex and fBitIndex
//if fBitIndex(highest to lowest) reaches 0 you need to switch to the next byte in the buffer. and also decrment fByteCount
//then finally return result
代码是
int IFStream12Bits::readBit()
{
if (fByteCount == 0){ reload(); }
//int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int result = 0;
cout << "bit: " << bit << endl;
//added this just cause
if (bit == 0)
{
result = 0;
}
else
{
result = 1;
}
//additional logic required?
fByteIndex++;
fBitIndex--;
//switch to next byte in the buffer
if (fBitIndex == 0)
{
fByteCount--;
fBitIndex = 8;
fByteIndex = 0;
}
return result;
}
如果您需要了解正在发生的事情,以下是完整的.cpp文件... IFStream12Bits.cpp
#include "IFStream12Bits.h"
#include <iostream>
using namespace std;
//default constructor
IFStream12Bits::IFStream12Bits()
{
init();
}
//takes aFIleName
IFStream12Bits::IFStream12Bits(const char* aFileName)
{
init();
open(aFileName);
}
//deconstructor
IFStream12Bits::~IFStream12Bits()
{
close();
}
//initialize the integer member variables with sensible values
//:fBuffer(), fByteCount(0), fByteIndex(0), fBitIndex(8)
//fBitIndex(highToLow)
void IFStream12Bits::init()
{
for (int i = 0; i < 32; i++)
{
fBuffer[i] = 0;
}
fByteCount = 0;
fByteIndex = 0;
fBitIndex = 8;
}
//fills input buffer fBuffer with the next 32 bytes and sets fByteCount to number of bytes read
void IFStream12Bits::reload()
{
//fills fBuffer with 32 bytes
fIStream.read((char*)fBuffer, 32);
//fIStream.read((char*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
//sets fByteCount to number of bytes read
fByteCount = fIStream.gcount();
}
//implements mapping process. returns 0 or 1 depending on value of fBuffer[fByteIndex] & (1 << (fBitIndex - 1))
//see how it works with experiments
//at start check if (fByteCount == 0){reload();} then use reload() called as buffer does not contain any data before calling reload
//next fetch the bit store and then advance fByteIndex and fBitIndex
//if fBitIndex(highest to lowest) reaches 0 you need to switch to the next byte in the buffer. and also decrment fByteCount
//then finally return result
int IFStream12Bits::readBit()
{
if (fByteCount == 0){ reload(); }
//int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int result = 0;
cout << "bit: " << bit << endl;
if (bit == 0)
{
result = 0;
}
else
{
result = 1;
}
//additional logic required?
fByteIndex++;
fBitIndex--;
//switch to next byte in the buffer
if (fBitIndex == 0)
{
fByteCount--;
fBitIndex = 8;
fByteIndex = 0;
}
return result;
}
void IFStream12Bits::open(const char* aFileName)
{
fIStream.open(aFileName, std::fstream::binary);
}
void IFStream12Bits::close()
{
fIStream.close();
}
bool IFStream12Bits::fail()
{
return fIStream.fail();
}
//true if no bytes left in input stream (fByteCount == 0)(should be zero if never read anythign from fIStream)
bool IFStream12Bits::eof()
{
return fByteCount == 0;
}
//read 12Bit codes from the bit input stream implements the read12Bits algorithm as shown in the tutorial
//basic readBit
//read12Bits(): 12Bit =
//declare Result : 12Bit = 0;
//for i = 1 to 12
//do
//declare lBit : Bit = get bit from input
//if(lBit == 1)
//then Result = (1 << (i-1)) + Result; //set bit at index i
//od
//return result
// multiply values by 2 to shift left???????????
IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
//int Result = a12BitValue;
//a12BitValue = ((a12BitValue & 0x0fff) << 1);
a12BitValue = a12BitValue & 0x0fff;
for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}
OFStream12Bits.cpp
#include "OFStream12Bits.h"
OFStream12Bits::OFStream12Bits()
{
init();
}
OFStream12Bits::OFStream12Bits(const char* aFileName)
{
init();
open(aFileName);
}
OFStream12Bits::~OFStream12Bits()
{
close();
}
void OFStream12Bits::init()
{
for (int i = 0; i < 32; i++)
{
fBuffer[i] = 0;
}
fByteIndex = 0;
fBitIndex = 8;
}
void OFStream12Bits::writeBit0()
{
fBitIndex--;
finishWriteBit();
}
void OFStream12Bits::writeBit1()
{
fBuffer[fByteIndex] += 1 << (fBitIndex - 1);
fBitIndex--;
finishWriteBit();
}
void OFStream12Bits::finishWriteBit()
{
if (fBitIndex == 0)
{
if (fByteIndex == 31)
{
fByteIndex++;
//write full buffer to stream
flush();
}
else
{
fByteIndex++;
fBitIndex = 8;
}
}
}
void OFStream12Bits::open(const char* aFileName)
{
fOStream.open(aFileName, std::ofstream::binary);
}
bool OFStream12Bits::fail()
{
return fOStream.fail();
}
void OFStream12Bits::close()
{
flush();
fOStream.close();
}
void OFStream12Bits::flush()
{
// do we need to add last byte?
fOStream.write((char*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
init();
}
OFStream12Bits& OFStream12Bits::operator<<(int a12BitValue)
{
a12BitValue = a12BitValue & 0x0fff; // mask 12 lower bits
for (int i = 0; i < 12; i++) //write 12 bits
{
if (a12BitValue & 0x01) // the current lowest bit is set
{
writeBit1();
}
else
{
writeBit0();
}
a12BitValue >>= 1; // code = code / 2 --shifting value accross
}
return *this;
}
的main.cpp
#include "OFStream12Bits.h"
#include "IFStream12Bits.h"
#include <iostream>
using namespace std;
void write4096()
{
cout << "Write 4096 codes" << endl;
OFStream12Bits lWriter("sample.lzw");
if (lWriter.fail())
{
cerr << "Error: unable to open output file" << endl;
exit(1);
}
for (int i = 4096; i >= 0; i--)
{
lWriter << i;
}
}
void read4096()
{
cout << "Read 4096 codes" << endl;
IFStream12Bits lInput("sample.lzw");
if (lInput.fail())
{
cerr << "Error: unable to open input file!" << endl;
exit(2);
}
for (int i = 4095; i >= 0; i--)
{
int l12BitValue;
lInput >> l12BitValue;
if (l12BitValue != i)
{
cerr << "Error: Code mismatch: " << l12BitValue << " != " << i << endl;
exit(3);
}
}
if (!lInput.eof())
{
cerr << "Error: Input stream not exhausted" << endl;
}
}
int main()
{
write4096();
read4096();
cout << "SUCCESS" << endl;
return 0;
}
答案 0 :(得分:1)
您的输入代码以前一个值开头。你应该从0开始,因为你没有清除未设置的位。
IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
a12BitValue = 0;
for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}
此外,+
将在这里工作,但在处理位时使用按位运算更清楚。此外,我认为你的转变是关闭的。我会写这样的设置位线:
a12BitValue |= 1 << i;
如果您考虑一下,当i
为0时,您需要设置第一位(1
或1 << 0
。)当i
为1时,你想要下一个,等等。所以你不需要减去一个。
我不确定这是唯一的问题,但您可以尝试使用单元测试独立测试每个类。例如,从原始字节缓冲区开始,如{0x89, 0xAB, 0xCD, 0xEF, 0x01}
,然后关闭三组12位。确认它们是正确的。然后创建一个空缓冲区,并向其写入特定位,并检查字节是否正确。
通过独立测试算法,并且输入/输出非常严格,您会发现更容易确定缺陷。