# Thread: How to get sequence of bits through Functional Call

1. ## How to get sequence of bits through Functional Call

Hi,

I need to get sequence of bits through Functional Call. Please look below code, its displaying only Single bit insted of sequence of bits.

Code:
```int PRBS()
{

unsigned int *b;
static unsigned lfsr  = 0xCD;
int i,j;
int bit;
int arr[50];

for ( i = 0; i < 50; i++)
{
bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4) ) & 1;
arr[i] = bit;
b=arr;
lfsr = (lfsr >> 1) | (bit << 7);
}
for (j = 0; j < 50; j++)
{
printf("%d", *b);
b++;
}
return (b);
}

int main()
{

unsigned int = result;
result=PRBS();
prinft("%d", result)
return 0;
}```

2. Originally Posted by SarasMuthu
Please look below code, its displaying only Single bit insted of sequence of bits.
I fixed the typo errors in your code, compiled and ran it, and got:
Code:
`110111111010011001101010001100000111010101011111001571555768`
The first part looks like a sequence of bits to me. The last part is because you botched the return value of the function: I have no idea what you actually want PRBS to return, but it surely isn't a pointer to unsigned int coerced into an int.

3. Thanks for your response. Sorry, i have uploaded wrong code, and now i am updating with New source code. Actually what i want means, in my source code, i need not use printf statement function. My result should be sequence of random bits 0`s and 1`s.

Code:
```int *Randombits( ) {

static int  reg;
int bit;
int i=0;

for (i=0;i<=32;i++)
{

bit  = ((reg >> 0) ^ (reg >> 2) ^ (reg >> 3) ^ (reg >> 5) ) & 1;
reg =  (reg >> 1) | (bit << 15);
reg [i] = bit;  /// SHOWING ERROR

}

return reg;  // SHOWING ERROR
}

/* main function to call above defined function */
int main () {

/* a pointer to an int */
int *p;
int i;

p = Randombits();

for ( i = 0; i < 30;i++ )
{
printf("*(p + [%d]) : %d\n", i, *(p + i) );
}

return 0;```

4. Then you need to figure out what in the world you actually want to return. It looks like you want to return a single random bit, but then of course you cannot print a sequence of random bits with a single call to PRBS. Perhaps:
Code:
```#include <stdio.h>

int PRBS(void)
{
static unsigned int lfsr = 0xCD;

int bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4)) & 1;
lfsr = (lfsr >> 1) | (bit << 7);
return bit;
}

int main(void)
{
int i;
for (i = 0; i < 50; i++)
{
putchar('0' + PRBS());
}
putchar('\n');
return 0;
}```

5. Thanks for your response. Sorry, i have uploaded wrong code, and now i am updating with New source code. Actually what i want means, in my source code, i need not use printf statement function. My result should be sequence of random bits 0`s and 1`s.
Code:
```int *Randombits( ) {

static int  reg;
int bit;
int i=0;

for (i=0;i<=32;i++)
{

bit  = ((reg >> 0) ^ (reg >> 2) ^ (reg >> 3) ^ (reg >> 5) ) & 1;
reg =  (reg >> 1) | (bit << 15);
reg [i] = bit;  /// SHOWING ERROR

}

return reg;  // SHOWING ERROR
}

/* main function to call above defined function */
int main () {

/* a pointer to an int */
int *p;
int i;

p = Randombits();

for ( i = 0; i < 30;i++ )
{
printf("*(p + [%d]) : %d\n", i, *(p + i) );
}

return 0;```

6. Try
Code:
```int *Randombits( ) {

static int  result[33];
int bit;
int i=0;
static int reg = 0xACF5;  // starting value, generates different patterns with each call to Randombits

for (i=0;i<=32;i++)
{

bit  = ((reg >> 0) ^ (reg >> 2) ^ (reg >> 3) ^ (reg >> 5) ) & 1;
reg =  (reg >> 1) | (bit << 15);
result [i] = bit;

}

return result;
}```

7. Originally Posted by SarasMuthu
Sorry, i have uploaded wrong code, and now i am updating with New source code.
Originally Posted by SarasMuthu
Sorry, i have uploaded wrong code, and now i am updating with New source code.
* waits for SarasMuthu to upload the new source code again as responding to yet another wrong code would be pointless *

8. Thanks. Finally, i have completed my task with all your help and guidance. My hardware part is now functioning..

9. Sorry guys, For me one more doubt for generating Random variable sequence why "static" keyword is necessary to declare with a variable in the program Code (Random variable sequence). Please explain me in according to above program codes.

Instead of "static", shall i use "unsigned char" in my program code.

Through Google search I found out " use of static keyword with a variable that is local to a function, it allows the last value of the variable to be preserved between successive calls to that function"