pnseed.c

来自「calculates Linear Feedback Shift Registe」· C语言 代码 · 共 70 行

C
70
字号
/*****************************************************************
 * Generic PN code shifter
 *   This program calculates Linear Feedback Shift Register(LFSR)
 *   mask and seed values for a specific phase shift.
 * Platform: Any gcc platform
 * Date: 12d/05m/00y
 *****************************************************************/

#include <stdio.h>
#include <string.h>

/*
 * Return true when the tested bit position contains binary 1;
 * otherwise return false.
 */
int testbit(long long number, int bitnum)
{
    return (int) ((number >> bitnum) & 1LL);
}

/*
 * Modify the specified bit position to the provided binary value;
 * return the resulted value.
 */
long long setbit(long long number, int bitnum, int set)
{
    return (number & (~(1LL << bitnum))) | ((long long)set << bitnum);
}

/*
 * Perform multiplication in Galois Field,
 * return the result.
 */
long long multi(long long poly, long long op1, long long op2)
{
    long long result;
    int i,n;

    n = sizeof(long long)*8-1;
    for (i=0; i<sizeof(long long)*8-1; i++)
    {
        if (((poly >> n) & 1LL) == 1LL) 
        {
            break;
        }
        n--;
    }
    if (n==0) 
    {
        return 0LL;
    }

    result = 0;
    for(i=0; i<n; i++)
    {
        if (testbit(op1,i) == 1) 
        {
            result ^= op2;
        }
        op2 <<= 1;
        if (testbit(op2,n) == 1) 
        {
            op2 ^= poly;
        }
    }
    return result;
}

/*
 * convolve with 憁atrix A

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?