Problem 1
Write a C++ program that writes all binary sequences of length 15
into a file binary15.txt.
-The sequences must be in increasing lexicographic order. The output filestream must be created with the command ofstream out("binary15.txt");
-Each sequence must be on a new line and the zeros and ones must not be separated by spaces. See below how the file must look like.
Beginning of file binary15.txt:
000000000000000
000000000000001
000000000000010
000000000000011
Problem 2
Write a C++ program that writes all nontrivial binary sequences of
length 15 with constant autocorrelation into a file binary15a.txt.
Hint: There are exactly 60 such sequences.
-The sequences must be in increasing lexicographic order. The output filestream must be created with the command ofstream out("binary15a.txt");
-The format of the file binary15a.txt is as in Problem 1.
The easiest way to resolve problem 1 is to increment an integer number from 0 to 2^15-1 in a loop. This number should be printed in a binary form in every iteration of the loop. For example:
1 2 3 4 5 6 7 8 9 10 11
for(int count = 0; count < 0x10000; count++)
{
// In the following loop we just basically print 'count' bit after bit.
for(int offset = 14; offset >= 0; offset--)
{
std::cout << ((count & (1 << offset)) >> offset);
}
std::cout << std::endl;
}
EDIT: I noticed that sequence was supposed to be written into a file. But you get the idea, I hope.
Thanks! I think this is the correct answer to write it into a file, is it? But can you explain further in detail on each steps coz I don't get it.. As you probably noticed, I totally suck at C++...
Damn, I just found a bug in my code. In the first line, 0x10000 (2^16) should be replaced with 0x8000 (2^15). Otherwise the sequence will be printed twice.
OK, now a little more details.
The first loop is self-explanatory, I think. I just increment 'count' from 0 (000000000000000 in a binary form) to 2^15-1 (111111111111111 in a binary form). The second loop prints 'count' in a binary form. How this is done? First, I AND 'count' with 0x4000 (the most significant bit of a 15-bit number). If the result is non-zero then '1' is printed. Otherwise '0' is printed. In the next step 'count' is AND'ed with the second most significant bit (0x2000), and the appropriate character is printed. The loop continues until all bits up to and including the least significant bit (0x0001) are printed. When it occurs, a newline character is printed and we start processing the next value of 'count'.