Translating Matlab scrambler function to C I am trying to implement the following matlab function in c:
Mathworks scrambler Function Documentation

This is the code I've written:

#define N 5
#define MAX 70
#define BITS 7
//char* msg = "This message is Coming to You!";
//char* msg = "Hello world 000\nHello world 001\nHello world 002\nHello world 003\n";
//char* msg = "Hello world 000\nHello world 001\n";
char* msg = "Hel";

uint8_t current[N-1] = {0};
uint8_t to_Xor[N-1] = {0};

uint8_t bin[MAX*BITS];

int main()
{
    int len=strlen(msg);
    int kk = 0;
    int m = 0;
    uint8_t letter = 0;
    uint8_t chars = 0;


    for(int nn = 0; nn &lt; len*BITS; nn++)
    {
        bin[nn] = 0;
    }

    for(int ii = 0; ii &lt; len; ii++)
    {
        letter = msg[ii];
        for(int jj = 6; jj&gt;=0; jj--)
        {
            bin[kk]=(letter&amp;(1&lt;&lt;jj))&gt;&gt;jj;
            //printf("%d\n",bin[k]);
            kk++;
        }

    }

    uint8_t xor = 0;
    uint8_t polynom = 0x0;
    uint8_t scrmbld_output[len*BITS];

    for(int c = 0; c &lt; len*BITS; c++)
    {
        scrmbld_output[c] = 0;
    }


    for(int bin_ndx = 0; bin_ndx &lt; len*BITS; bin_ndx++)
    {

        for(int n = 0; n &lt; N-1; n++)
        {
            if((1&lt;&lt;n)&amp;polynom)
            {
                xor ^= current[n];
            }
        }

        for (int k = N-2; k &gt; 0; k--)
        {
            current[k]=current[k-1];
        }

        current[0] = xor ^ bin[bin_ndx] ^ 1;

        scrmbld_output[bin_ndx] = current[0];
        xor = 0;

        printf("original: %d scrmbld: %d\n",bin[bin_ndx], scrmbld_output[bin_ndx]);
    }

    int k = 0;
    m = 6;

    for(int i = 0; i &lt; len; i++)
    {
        for(int j = 6; j&gt;=0; j--)
        {
            chars |= bin[k]&lt;&lt;m;
            k++;
            m--;
        }
        printf("%c", chars);
        chars = 0;
        m = 6;
    }

    printf("\nlen: %d", len);
    return 0;
}


The results I get from matlab are different.

If I create the following vector and scrambler in matlab:

a = [ 1     0     0     1     0     0     0     1     1     0 ];
scrmblr = comm.Scrambler(2,[1 1 1 0 1], [0 0 0 0]);
ans = scrmblr(a');
ans' = [1     1     0     0     1     0     1     0     1     1]


but what I'm getting is:

original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 1 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 1
original: 1 scrmbld: 1
original: 1 scrmbld: 1
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 1 scrmbld: 0
original: 1 scrmbld: 0
original: 1 scrmbld: 1
original: 0 scrmbld: 1
original: 1 scrmbld: 1
original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0


I don't see where I went wrong. Any help greatly appreciated, thank you!
I think the mistake would be this line

current[0] = xor ^ bin[bin_ndx] ^ 1;


Remove the xor with 1 at the end.

current[0] = xor ^ bin[bin_ndx];


The MATLAB figure shows 1 because that is the multiplier (scale factor) and may not mean xor with 1.