📜  Hamming代码在C / C++中的实现

📅  最后修改于: 2021-05-25 03:36:53             🧑  作者: Mango

先决条件:汉明码

给定一个消息位,其形式为msgBit []数组,则任务是查找给定消息位的汉明码。

例子:

方法:思想是首先找到其可通过同时2 r为小于每次初始化被1个R,然后通过递增1被发现冗余位的数目(M + R + 1),其中m是比特数在输入消息中。请按照以下步骤解决问题:

  • r初始化为1,然后将其递增1,直到2 r小于m + r + 1
  • 初始化大小为r + m的向量hammingCode ,该向量将为输出消息的长度。
  • 1和设置汉明码[2 I 通过遍历从i = 0至r初始化-1冗余比特的所有的位置 1] = -1 。然后将输入消息位放在hammingCode [j]不为-1的所有位置,顺序为0 <= j <(r + m)
  • 0初始化一个变量one_count来存储1的数量,然后从i = 0遍历到(r + m – 1)
  • 如果当前位即汉明码[i]-1然后找到的消息位含有组位在日志2 =1如果递增one_count第(i + 1)通过从j横动位置i + 2到R + M (j&(1 << x))不为0,hammingCode [j – 1]1
  • 如果对于索引ione_count是偶数,则设置hammingCode [i] = 0,否则设置hammingCode [i] = 1
  • 遍历后,将hammingCode []向量打印为输出消息。

下面是上述方法的实现:

C
// C program for the above approach
  
#include 
#include 
  
// Store input bits
int input[32];
  
// Store hamming code
int code[32];
  
int ham_calc(int, int);
void solve(int input[], int);
  
// Function to calculate bit for
// ith position
int ham_calc(int position, int c_l)
{
    int count = 0, i, j;
    i = position - 1;
  
    // Traverse to store Hamming Code
    while (i < c_l) {
  
        for (j = i; j < i + position; j++) {
  
            // If current boit is 1
            if (code[j] == 1)
                count++;
        }
  
        // Update i
        i = i + 2 * position;
    }
  
    if (count % 2 == 0)
        return 0;
    else
        return 1;
}
  
// Function to calculate hamming code
void solve(int input[], int n)
{
    int i, p_n = 0, c_l, j, k;
    i = 0;
  
    // Find msg bits having set bit
    // at x'th position of number
    while (n > (int)pow(2, i) - (i + 1)) {
        p_n++;
        i++;
    }
  
    c_l = p_n + n;
  
    j = k = 0;
  
    // Traverse the msgBits
    for (i = 0; i < c_l; i++) {
  
        // Update the code
        if (i == ((int)pow(2, k) - 1)) {
            code[i] = 0;
            k++;
        }
  
        // Update the code[i] to the
        // input character at index j
        else {
            code[i] = input[j];
            j++;
        }
    }
  
    // Traverse and update the
    // hamming code
    for (i = 0; i < p_n; i++) {
  
        // Find current position
        int position = (int)pow(2, i);
  
        // Find value at current position
        int value = ham_calc(position, c_l);
  
        // Update the code
        code[position - 1] = value;
    }
  
    // Print the Hamming Code
    printf("\nThe generated Code Word is: ");
    for (i = 0; i < c_l; i++) {
        printf("%d", code[i]);
    }
}
  
// Driver Code
void main()
{
    // Given input message Bit
    input[0] = 0;
    input[1] = 1;
    input[2] = 1;
    input[3] = 1;
  
    int N = 4;
  
    // Function Call
    solve(input, N);
}


C++
// C++ program for the above approach
  
#include 
using namespace std;
  
// Function to generate hamming code
vector generateHammingCode(
    vector msgBits, int m, int r)
{
    // Stores the Hamming Code
    vector hammingCode(r + m);
  
    // Find positions of redundant bits
    for (int i = 0; i < r; ++i) {
  
        // Placing -1 at redundant bits
        // place to identify it later
        hammingCode[pow(2, i) - 1] = -1;
    }
  
    int j = 0;
  
    // Iterate to update the code
    for (int i = 0; i < (r + m); i++) {
  
        // Placing msgBits where -1 is
        // absent i.e., except redundant
        // bits all postions are msgBits
        if (hammingCode[i] != -1) {
            hammingCode[i] = msgBits[j];
            j++;
        }
    }
  
    for (int i = 0; i < (r + m); i++) {
  
        // If current bit is not redundant
        // bit then continue
        if (hammingCode[i] != -1)
            continue;
  
        int x = log2(i + 1);
        int one_count = 0;
  
        // Find msg bits containing
        // set bit at x'th position
        for (int j = i + 2;
             j <= (r + m); ++j) {
  
            if (j & (1 << x)) {
                if (hammingCode[j - 1] == 1) {
                    one_count++;
                }
            }
        }
  
        // Generating hamming code for
        // even parity
        if (one_count % 2 == 0) {
            hammingCode[i] = 0;
        }
        else {
            hammingCode[i] = 1;
        }
    }
  
    // Return the generated code
    return hammingCode;
}
  
// Function to find the hamming code
// of the given message bit msgBit[]
void findHammingCode(vector& msgBit)
{
  
    // Message bit size
    int m = msgBit.size();
  
    // r is the number of redundant bits
    int r = 1;
  
    // Find no. of redundant bits
    while (pow(2, r) < (m + r + 1)) {
        r++;
    }
  
    // Generating Code
    vector ans
        = generateHammingCode(msgBit, m, r);
  
    // Print the code
    cout << "Message bits are: ";
    for (int i = 0; i < msgBit.size(); i++)
        cout << msgBit[i] << " ";
  
    cout << "\nHamming code is: ";
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << " ";
}
  
// Driver Code
int main()
{
    // Given message bits
    vector msgBit = { 0, 1, 0, 1 };
  
    // Function Call
    findHammingCode(msgBit);
  
    return 0;
}


输出:
The generated Code Word is: 0001111

时间复杂度: O((M + R) 2 ),其中M是输入消息中的位数,R是冗余位数
辅助空间: O(M + R)

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”