#include
<
iostream
>
#include < exception >
using namespace std;
namespace Encode
... {
class LengthException : public std::exception
...{
virtual const char* what()
...{
return "not expected length";
}
};
/**//**
* @brief encode a less than 26-bit int to hamming code
* @param inCode IN as orginal code
* @param inLen IN as the length infomation
* @param outCode OUT as the encoded int
* @param outLen OUT as the length
* @return none
*/
void Hamming26(int const inCode, int const inLen, int& outCode, int& outLen)
...{
int checkNum = 2;
if (inLen > 26)
throw LengthException();
while (((1 << checkNum) - checkNum - 1) < inLen)//保证2r>m+r+1
...{
++ checkNum;
}
outLen = checkNum + inLen;
int checksum[5] = ...{0,0,0,0,0}; // 26-bit int will not need more than 5 checkpositions
int code = inCode;
outCode = 0;
int de = checksum[4];
// first move original code to the result code
for (int i = 1, test = 1, index = 0; i <= outLen; ++i)
...{
if (i& test)
...{
test <<= 1; // if i == 1|2|4|8... then jump over
}
else
...{
int pos = i; // position in the outCode
int value = (code & 0x00000001) << (pos-1);
for (int index = 0;pos; ++ index) // in this for-loop we get the check number
...{
if (pos & 0x00000001) // should be checked by this check-group
...{
if(value) // if this bit is 1
...{
checksum[index] = !checksum[index]; // xor
}
}
pos >>= 1;
}
outCode |= value;
code >>= 1;
}
}
for (int i = 0; i < checkNum; ++i)
...{
outCode |= (checksum << ((1 << i)-1));
}
}
/**//**
* @brief check the correctness of a hamming code
* @param code received code
* @param length the length of the code
* @return return -1 if no error, or return the position of the error bit
*/
int CheckCode(int code, int length)
...{
int checkNum = 1;
while((1 << checkNum) < length)
++ checkNum;
int checksum[5] = ...{0,0,0,0,0};
for (int i = 1; i <= length; ++ i)
...{
int pos = i;
int value = code & (1 << (pos - 1));
for (int index = 0; pos; ++ index)
...{
if (pos & 0x00000001) // need to be checked by this group
...{
if (value)
...{
checksum[index] = !checksum[index];
}
}
pos >>= 1;
}
}
/**//** for test
for (int i = 0; i < checkNum; ++ i)
{
cout << checksum;
}
*/
int ret = 0;
for (int i = checkNum - 1; i >= 0; -- i)
...{
ret <<= 1;
ret |= checksum;
}
return ret;
}
}
int main()
... {
int code = 0x03FFFFFF;
int length = 26;
int result, reLen;
Encode::Hamming26(code, length, result, reLen);
cout << "The orignal code ";
// display from LSB to MSB
for (int i = 0; i < length; ++ i)
...{
cout << ((code & (1 << i)) ? 1 : 0);
}
cout << " the hamming code ";
for (int i = 0; i < reLen; ++ i)
...{
cout << ((result & (1 << i)) ? 1 : 0);
}
cout << " now to test the result ";
// test and indicate which bit is wrong
// notice the binary number should be reversed
// when you want to know the decimal value
for (int i = 0; i < reLen; ++ i)
...{
int test = result & ~(1 << i);// result & ( 1 << i);
cout << Encode::CheckCode(test,reLen);
cout << ' ';
}
getchar();
return 0;
}
#include < exception >
using namespace std;
namespace Encode
... {
class LengthException : public std::exception
...{
virtual const char* what()
...{
return "not expected length";
}
};
/**//**
* @brief encode a less than 26-bit int to hamming code
* @param inCode IN as orginal code
* @param inLen IN as the length infomation
* @param outCode OUT as the encoded int
* @param outLen OUT as the length
* @return none
*/
void Hamming26(int const inCode, int const inLen, int& outCode, int& outLen)
...{
int checkNum = 2;
if (inLen > 26)
throw LengthException();
while (((1 << checkNum) - checkNum - 1) < inLen)//保证2r>m+r+1
...{
++ checkNum;
}
outLen = checkNum + inLen;
int checksum[5] = ...{0,0,0,0,0}; // 26-bit int will not need more than 5 checkpositions
int code = inCode;
outCode = 0;
int de = checksum[4];
// first move original code to the result code
for (int i = 1, test = 1, index = 0; i <= outLen; ++i)
...{
if (i& test)
...{
test <<= 1; // if i == 1|2|4|8... then jump over
}
else
...{
int pos = i; // position in the outCode
int value = (code & 0x00000001) << (pos-1);
for (int index = 0;pos; ++ index) // in this for-loop we get the check number
...{
if (pos & 0x00000001) // should be checked by this check-group
...{
if(value) // if this bit is 1
...{
checksum[index] = !checksum[index]; // xor
}
}
pos >>= 1;
}
outCode |= value;
code >>= 1;
}
}
for (int i = 0; i < checkNum; ++i)
...{
outCode |= (checksum << ((1 << i)-1));
}
}
/**//**
* @brief check the correctness of a hamming code
* @param code received code
* @param length the length of the code
* @return return -1 if no error, or return the position of the error bit
*/
int CheckCode(int code, int length)
...{
int checkNum = 1;
while((1 << checkNum) < length)
++ checkNum;
int checksum[5] = ...{0,0,0,0,0};
for (int i = 1; i <= length; ++ i)
...{
int pos = i;
int value = code & (1 << (pos - 1));
for (int index = 0; pos; ++ index)
...{
if (pos & 0x00000001) // need to be checked by this group
...{
if (value)
...{
checksum[index] = !checksum[index];
}
}
pos >>= 1;
}
}
/**//** for test
for (int i = 0; i < checkNum; ++ i)
{
cout << checksum;
}
*/
int ret = 0;
for (int i = checkNum - 1; i >= 0; -- i)
...{
ret <<= 1;
ret |= checksum;
}
return ret;
}
}
int main()
... {
int code = 0x03FFFFFF;
int length = 26;
int result, reLen;
Encode::Hamming26(code, length, result, reLen);
cout << "The orignal code ";
// display from LSB to MSB
for (int i = 0; i < length; ++ i)
...{
cout << ((code & (1 << i)) ? 1 : 0);
}
cout << " the hamming code ";
for (int i = 0; i < reLen; ++ i)
...{
cout << ((result & (1 << i)) ? 1 : 0);
}
cout << " now to test the result ";
// test and indicate which bit is wrong
// notice the binary number should be reversed
// when you want to know the decimal value
for (int i = 0; i < reLen; ++ i)
...{
int test = result & ~(1 << i);// result & ( 1 << i);
cout << Encode::CheckCode(test,reLen);
cout << ' ';
}
getchar();
return 0;
}