By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Random Binary Sequence

P: 2
Hello everyone! I'm searching for a Random Binary Sequence which creates the
outcome of an XOR gate. I need to create a sequence of 2^(7) -1 bits....using my
C compiler. Thank you all for your time...
Jun 29 '06 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
Sorry ut that doesn't make sense to me, since XOR is definately not random I fail to see how a Random Binary Sequence could be the output of XOR.

Perhaps you could explain the problem in more detail.
Jun 30 '06 #2

P: 2
Thanx for your interest. I need to create a Random Binary Sequence starting with
four '1' (bits), that is to say 1 1 1 1. We now assume that the two last bits are inputs of an XOR gate. We calculate their outcome which is 0. The next sequence we receive is 0 1 1 1, which results from the movement of 0 into the first position and the movement of the three first bits by one position to the right, just like if we had a shift register. We are doing this procedure many times and at last we receive the last column. I mean that each time we need to keep only the last of the four
bits. Now, if we need to receive 2^(7)-1 bits we must calculate 127 times the outcome of the XOR gate and receive each time the last bit, that is to say
127 bits.
Hope not to ask too much!!!! I think that such a function exists on Basic, but I
need to do it in C. Thanks again!!! :-)
Jun 30 '06 #3

100+
P: 293
D_C
The following code is a terrible way to do this, using only XOR for the booleans. There's a much nicer way to do it, I'll leave it up to you to figure out what the code is doing, and how to make it nicer.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   bool input[] = {true,true,true,true};
  4.   int i;
  5.  
  6.   for(i = 0; i < 127; i++)
  7.   {
  8.     input[0] ^= input[1];
  9.     input[1] ^= input[0];
  10.     input[0] ^= input[1];
  11.     input[2] ^= input[3];
  12.     input[3] ^= input[2];
  13.     input[0] ^= input[2];
  14.     input[2] ^= input[0];
  15.     input[0] ^= input[2];
  16.  
  17.     if(input[0])
  18.       cout << "1";
  19.     else
  20.       cout << "0";
  21.   }
  22.   cout << endl;
  23.   system("PAUSE");
  24.   return EXIT_SUCCESS;
  25. }
Ignoring the spaces, the result is: 000100110101111 repeated eight and a half times. I think chaotic is a better word than random.
Jun 30 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
OK well I think something like this is what you want

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6.     unsigned char data = 0xff;
  7.     unsigned char result;
  8.     int ix;
  9.  
  10.     for(ix=0;ix<127;ix++)
  11.     {
  12.         result = (data ^ ((data & 0x40) << 1)) & 0x80;
  13.  
  14.         if ( result & 0x80 )
  15.         {
  16.             putchar('1');
  17.         }
  18.         else
  19.         {
  20.             putchar('0');
  21.         }
  22.  
  23.         data = (data >> 1) | result;
  24.     }
  25.  
  26.     putchar('\n');
  27.  
  28.     return 0;
  29. }
  30.  
But the results are not very random

01101101101101101101101101101101101101101101101101 10110110110110110110110110110110110110110110110110 110110110110110110110110110

As you can see.
Jun 30 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Ignoring the spaces, the result is: 000100110101111 repeated eight and a half times. I think chaotic is a better word than random.
Actually chaotic is a really poor word for it,a chaotic system never repeats which you admit yours does.

Still trying to work out what your code actualy does though :D
Jun 30 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.