简体   繁体   中英

all possible combinations bits

I am working on a program in C++ to demonstrate the workings of coding theory (in the sense of error correction using linear codes). I am adding parity bits to a string of bits ('words'). This is so I can still see what the message used to be if some bits have changed during transmission (Error detection and correction). One important thing to know is the minimum distance between two words. To calculate this I need to compile a list of all possible words and compare them to each other. If my error correction code consists of words of length n=6, there would be 2^6 = 64 possible combinations. My question is about how I can generate all these possible words and store them in an array.

These are two instances of what these words would look like:

0 0 0 0 0 0
1 0 0 0 0 0
1 1 0 1 0 1

I know I can generate combinations of two numbers with an algorithm like this:

   for (int i = 1; i <= 5; i++)
        for (int j = 2; j <= 5; j++)
            if (i != j)
                cout << i << "," << j << "," << endl;

However, this code only generates combinations of two numbers and also uses numbers other than 1 or 0.

EDIT

I have created a few for loops that do the job. It is not especially elegant:

int bits[64][6] = { 0 };

for (int x = 0; x < 32; x++)
    bits[x][0] = 1;

for (int x = 0; x < 64; x += 2)
    bits[x][1] = 1;

for (int x = 0; x < 64; x += 4)
{
    bits[x][2] = 1;
    bits[x + 1][2] = 1;
}

for (int x = 0; x < 64; x += 8)
{
    bits[x][3] = 1;
    bits[x + 1][3] = 1;
    bits[x + 2][3] = 1;
    bits[x + 3][3] = 1;
}

for (int x = 0; x < 64; x += 16)
{
    for (int i = 0; i < 8; i++)
        bits[x + i][4] = 1;
}

for (int x = 0; x < 64; x += 32)
{
    for (int i = 0; i < 16; i++)
        bits[x + i][5] = 1;
}

You may use the following: http://ideone.com/C8O8Qe

template <std::size_t N>
bool increase(std::bitset<N>& bs)
{
    for (std::size_t i = 0; i != bs.size(); ++i) {
        if (bs.flip(i).test(i) == true) {
            return true;
        }
    }
    return false; // overflow
}

And then to iterate on all values :

std::bitset<5> bs;

do {
    std::cout << bs << std::endl;
} while (increase(bs));

If size is not a compile time value, you may use similar code with std::vector<bool>

I'd use iota or similar:

vector<int> foo(64); // Create a vector to hold 64 entries

iota(foo.begin(), foo.end(), 0); // Inserts the range of numbers in foo [0,foo.size())

for(auto& i : foo){
    cout << bitset<6>(i) << endl;
}

I should probably also point out that an int is a sizeof(int) collection of bits, so hopefully you can work with that using bit-wise operators.

If you must use a more literal collection of bits, I would second Jarod42 's answer, but still use iota :

vector<bitset<6>> bar(64);

iota(bar.begin(), bar.end(), 0);

for(auto& i : bar){
    cout << i << endl;
}

Use a double loop from 0 to 62 and from the first loop index to 63.

Inside the loops convert the two indexes to binary. (A simple way is to convert to hexadecimal and expand the hex digits into four bits.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM