Thread: How to get sequence of bits through Functional Call

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    52

    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.

    Please debug my code, and suggest me . Thanks in advance.

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,922
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    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  load_data =0xACF5;
    int i=0;
     reg = load_data;
    
    
    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;
    Last edited by SarasMuthu; 05-17-2017 at 03:07 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,922
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    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  load_data =0xACF5;
    int i=0;
     reg = load_data;
     
     
    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;
    How to get sequence of bits through Functional Call-c_code-png

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,169
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,922
    Quote Originally Posted by SarasMuthu View Post
    Sorry, i have uploaded wrong code, and now i am updating with New source code.
    Quote Originally Posted by SarasMuthu View Post
    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 *
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Thanks. Finally, i have completed my task with all your help and guidance. My hardware part is now functioning..

  9. #9
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    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"

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,169
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 05-02-2017, 05:28 PM
  2. Functional call != stack creation?
    By MK27 in forum C Programming
    Replies: 7
    Last Post: 12-22-2009, 11:47 PM
  3. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  4. Functional programming languages... r they really functional?
    By code_mutant in forum C++ Programming
    Replies: 10
    Last Post: 02-25-2004, 05:29 AM
  5. Replies: 4
    Last Post: 07-05-2002, 08:03 AM

Tags for this Thread