📜  C/C++汉明码实现

📅  最后修改于: 2021-09-03 04:08:25             🧑  作者: 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)
  • 如果当前位,即hammingCode[i]不是-1,则通过从j = i+2遍历到 r+m,将one_count增加1,在 log 2 (i+1) th位置找到包含设置位的消息位,如果(j & (1<不是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++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程